Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️ use endpoint url from service account #5

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ func privateKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) {
}
}

func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, error) {
func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, *serviceAccountCredentials, error) {
var credentials *serviceAccountCredentials
err := json.Unmarshal(data, &credentials)
if credentials == nil || err != nil {
return nil, errors.New("valid service account needs to be provided")
return nil, nil, errors.New("valid service account needs to be provided")
}

// verify that we can read the private key
privateKey, err := privateKeyFromBytes([]byte(credentials.PrivateKey))
if err != nil {
return nil, errors.New("cannot load retrieved key: " + err.Error())
return nil, nil, errors.New("cannot load retrieved key: " + err.Error())
}

// configure authentication plugin, since the server only accepts authenticated calls
Expand All @@ -75,7 +75,7 @@ func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, erro

return &serviceAccountTokenSource{
cfg: cfg,
}, nil
}, credentials, nil
}

type tokenSourceConfig struct {
Expand Down
22 changes: 13 additions & 9 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,46 @@ func (w withHTTPClient) Apply(o *internal.DialSettings) {

// WithTokenSource returns a ClientOption that specifies the oauth2.TokenSource
func WithTokenSource(s oauth2.TokenSource) ClientOption {
return withTokenSource{s, nil}
return withTokenSource{"", s, nil}
}

type withTokenSource struct {
ts oauth2.TokenSource
err error
endpoint string
ts oauth2.TokenSource
err error
}

func (w withTokenSource) Apply(o *internal.DialSettings) {
o.TokenSource = w.ts
o.TokenError = w.err
if w.endpoint != "" {
o.Endpoint = w.endpoint + "/query"
}
}

// WithAPIToken returns a ClientOption that specifies the oauth2.TokenSource with the given token.
func WithAPIToken(token string) ClientOption {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return withTokenSource{src, nil}
return withTokenSource{"", src, nil}
}

// WithServiceAccount returns a ClientOption that specifies the credentials file to use.
func WithServiceAccount(data []byte) ClientOption {
ts, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{ts, err}
ts, sa, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{sa.ApiEndpoint, ts, err}
}

// WithServiceAccountFile returns a ClientOption that specifies the credentials file to use.
func WithServiceAccountFile(filename string) ClientOption {
data, err := os.ReadFile(filename)
if err != nil {
return withTokenSource{nil, err}
return withTokenSource{"", nil, err}
}

ts, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{ts, err}
ts, sa, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{sa.ApiEndpoint, ts, err}
}

// WithoutAuthentication returns a ClientOption that disables authentication.
Expand Down
Loading