-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from shmel1k/feature/add_metadata_credentials_…
…provider Feature/add metadata credentials provider
- Loading branch information
Showing
8 changed files
with
1,416 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package credentials | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/credentials" | ||
yc "github.com/ydb-platform/ydb-go-yc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type iamCredsProvider struct { | ||
keyfileName string | ||
iamEndpoint string | ||
|
||
once sync.Once | ||
creds credentials.Credentials | ||
initErr error | ||
} | ||
|
||
// ContextWithAuth implements Provider. | ||
func (i *iamCredsProvider) ContextWithAuth(ctx context.Context) (context.Context, context.CancelFunc) { | ||
// TODO(shmel1k@): add error handling | ||
tok, _ := i.GetToken() | ||
ctx, cf := context.WithCancel(ctx) | ||
return metadata.AppendToOutgoingContext(ctx, | ||
"x-ydb-auth-ticket", tok), cf | ||
} | ||
|
||
// ContextWithoutAuth implements Provider. | ||
func (i *iamCredsProvider) ContextWithoutAuth(ctx context.Context) (context.Context, context.CancelFunc) { | ||
// TODO(shmel1k@): remove ContextWithoutAuth method | ||
return context.WithCancel(ctx) | ||
} | ||
|
||
// GetToken implements Provider. | ||
func (i *iamCredsProvider) GetToken() (string, error) { | ||
return i.creds.Token(context.TODO()) | ||
} | ||
|
||
// Init implements Provider. | ||
func (i *iamCredsProvider) Init() error { | ||
i.once.Do(func() { | ||
sp := strings.Split(i.iamEndpoint, ":") | ||
if len(sp) == 1 { | ||
i.iamEndpoint += ":443" | ||
} | ||
i.creds, i.initErr = yc.NewClient( | ||
yc.WithServiceFile(i.keyfileName), | ||
yc.WithEndpoint(i.iamEndpoint), | ||
) | ||
}) | ||
return i.initErr | ||
} | ||
|
||
func NewIamCreds(keyfileName, iamEndpoint string) Provider { | ||
return &iamCredsProvider{ | ||
keyfileName: keyfileName, | ||
iamEndpoint: iamEndpoint, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package credentials | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
yc "github.com/ydb-platform/ydb-go-yc-metadata" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type metadataProvider struct { | ||
once sync.Once | ||
creds *yc.InstanceServiceAccountCredentials | ||
} | ||
|
||
// ContextWithAuth implements Provider. | ||
func (m *metadataProvider) ContextWithAuth(ctx context.Context) (context.Context, context.CancelFunc) { | ||
token, _ := m.GetToken() // TODO(shmel1k@): add error handling | ||
ctx1, cf := context.WithCancel(ctx) | ||
return metadata.AppendToOutgoingContext(ctx1, "x-ydb-auth-ticket", token), cf | ||
} | ||
|
||
// ContextWithoutAuth implements Provider. | ||
func (m *metadataProvider) ContextWithoutAuth(ctx context.Context) (context.Context, context.CancelFunc) { | ||
return context.WithCancel(ctx) | ||
} | ||
|
||
// GetToken implements Provider. | ||
func (m *metadataProvider) GetToken() (string, error) { | ||
// TODO(shmel1k@): add contexts. | ||
return m.creds.Token(context.TODO()) | ||
} | ||
|
||
// Init implements Provider. | ||
func (m *metadataProvider) Init() error { | ||
m.once.Do(func() { | ||
m.creds = yc.NewInstanceServiceAccount(yc.WithURL("http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token")) | ||
}) | ||
return nil | ||
} | ||
|
||
func NewMetadata(logger *zap.SugaredLogger) Provider { | ||
return &metadataProvider{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.