-
Notifications
You must be signed in to change notification settings - Fork 94
PBM-1504 Add support for Workload Identity Authentication #1021
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
base: dev
Are you sure you want to change the base?
Changes from all commits
2597f10
5717fc5
d92c8f4
75a0639
a61a341
f6f25fe
137c32f
72abb20
e3e6387
7692401
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ import ( | |
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
"github.com/aws/aws-sdk-go/service/sts" | ||
"golang.org/x/oauth2/google" | ||
|
||
"github.com/percona/percona-backup-mongodb/pbm/errors" | ||
"github.com/percona/percona-backup-mongodb/pbm/log" | ||
|
@@ -43,6 +44,7 @@ type Config struct { | |
Provider string `bson:"provider,omitempty" json:"provider,omitempty" yaml:"provider,omitempty"` | ||
Region string `bson:"region" json:"region" yaml:"region"` | ||
EndpointURL string `bson:"endpointUrl,omitempty" json:"endpointUrl" yaml:"endpointUrl,omitempty"` | ||
ServiceAccount string `bson:"serviceAccount,omitempty" json:"serviceAccount" yaml:"serviceAccount,omitempty"` | ||
ForcePathStyle *bool `bson:"forcePathStyle,omitempty" json:"forcePathStyle,omitempty" yaml:"forcePathStyle,omitempty"` | ||
Bucket string `bson:"bucket" json:"bucket" yaml:"bucket"` | ||
Prefix string `bson:"prefix,omitempty" json:"prefix,omitempty" yaml:"prefix,omitempty"` | ||
|
@@ -159,6 +161,9 @@ func (cfg *Config) Equal(other *Config) bool { | |
if cfg.EndpointURL != other.EndpointURL { | ||
return false | ||
} | ||
if cfg.ServiceAccount != other.ServiceAccount { | ||
return false | ||
} | ||
if cfg.Bucket != other.Bucket { | ||
return false | ||
} | ||
|
@@ -196,6 +201,9 @@ func (cfg *Config) Cast() error { | |
if cfg.Region == "" { | ||
cfg.Region = defaultS3Region | ||
} | ||
if cfg.ServiceAccount == "" { | ||
cfg.ServiceAccount = "default" | ||
} | ||
if cfg.ForcePathStyle == nil { | ||
cfg.ForcePathStyle = aws.Bool(true) | ||
} | ||
|
@@ -550,6 +558,20 @@ func (s *S3) session() (*session.Session, error) { | |
}}) | ||
} | ||
|
||
// If using GCE, attempt to retrieve access token from metadata server | ||
if onGCE() { | ||
tokenSource := google.ComputeTokenSource(s.opts.ServiceAccount, "") | ||
token, err := tokenSource.Token() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "get GCP token") | ||
} | ||
providers = append(providers, &credentials.StaticProvider{Value: credentials.Value{ | ||
AccessKeyID: "GCP_OAUTH_TOKEN", | ||
SecretAccessKey: "GCP_OATH_TOKEN", | ||
SessionToken: token.AccessToken, | ||
}}) | ||
} | ||
|
||
awsSession, err := session.NewSession() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "new session") | ||
|
@@ -617,3 +639,24 @@ func awsLogger(l log.LogEvent) aws.Logger { | |
l.Debug(msg, xs...) | ||
}) | ||
} | ||
|
||
func onGCE() bool { | ||
client := http.Client{ | ||
Timeout: 100 * time.Millisecond, | ||
} | ||
|
||
req, err := http.NewRequest("GET", "http://169.254.169.254/computeMetadata/v1/", nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't "http://metadata.google.internal" be used instead of the hardcoded IP? |
||
if err != nil { | ||
return false | ||
} | ||
|
||
req.Header.Add("Metadata-Flavor", "Google") | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return false | ||
} | ||
defer resp.Body.Close() | ||
|
||
return resp.StatusCode == http.StatusOK | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
GCP_OATH_TOKEN
a typo?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, no not a typo, the fields are mandatory but they're just set to placeholder values here since they're not used for GCS.