externalaccount.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package externalaccount
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "net/http"
  20. "regexp"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "cloud.google.com/go/auth"
  25. "cloud.google.com/go/auth/credentials/internal/impersonate"
  26. "cloud.google.com/go/auth/credentials/internal/stsexchange"
  27. "cloud.google.com/go/auth/internal/credsfile"
  28. )
  29. const (
  30. timeoutMinimum = 5 * time.Second
  31. timeoutMaximum = 120 * time.Second
  32. universeDomainPlaceholder = "UNIVERSE_DOMAIN"
  33. defaultTokenURL = "https://sts.UNIVERSE_DOMAIN/v1/token"
  34. defaultUniverseDomain = "googleapis.com"
  35. )
  36. var (
  37. // Now aliases time.Now for testing
  38. Now = func() time.Time {
  39. return time.Now().UTC()
  40. }
  41. validWorkforceAudiencePattern *regexp.Regexp = regexp.MustCompile(`//iam\.googleapis\.com/locations/[^/]+/workforcePools/`)
  42. )
  43. // Options stores the configuration for fetching tokens with external credentials.
  44. type Options struct {
  45. // Audience is the Secure Token Service (STS) audience which contains the resource name for the workload
  46. // identity pool or the workforce pool and the provider identifier in that pool.
  47. Audience string
  48. // SubjectTokenType is the STS token type based on the Oauth2.0 token exchange spec
  49. // e.g. `urn:ietf:params:oauth:token-type:jwt`.
  50. SubjectTokenType string
  51. // TokenURL is the STS token exchange endpoint.
  52. TokenURL string
  53. // TokenInfoURL is the token_info endpoint used to retrieve the account related information (
  54. // user attributes like account identifier, eg. email, username, uid, etc). This is
  55. // needed for gCloud session account identification.
  56. TokenInfoURL string
  57. // ServiceAccountImpersonationURL is the URL for the service account impersonation request. This is only
  58. // required for workload identity pools when APIs to be accessed have not integrated with UberMint.
  59. ServiceAccountImpersonationURL string
  60. // ServiceAccountImpersonationLifetimeSeconds is the number of seconds the service account impersonation
  61. // token will be valid for.
  62. ServiceAccountImpersonationLifetimeSeconds int
  63. // ClientSecret is currently only required if token_info endpoint also
  64. // needs to be called with the generated GCP access token. When provided, STS will be
  65. // called with additional basic authentication using client_id as username and client_secret as password.
  66. ClientSecret string
  67. // ClientID is only required in conjunction with ClientSecret, as described above.
  68. ClientID string
  69. // CredentialSource contains the necessary information to retrieve the token itself, as well
  70. // as some environmental information.
  71. CredentialSource *credsfile.CredentialSource
  72. // QuotaProjectID is injected by gCloud. If the value is non-empty, the Auth libraries
  73. // will set the x-goog-user-project which overrides the project associated with the credentials.
  74. QuotaProjectID string
  75. // Scopes contains the desired scopes for the returned access token.
  76. Scopes []string
  77. // WorkforcePoolUserProject should be set when it is a workforce pool and
  78. // not a workload identity pool. The underlying principal must still have
  79. // serviceusage.services.use IAM permission to use the project for
  80. // billing/quota. Optional.
  81. WorkforcePoolUserProject string
  82. // UniverseDomain is the default service domain for a given Cloud universe.
  83. // This value will be used in the default STS token URL. The default value
  84. // is "googleapis.com". It will not be used if TokenURL is set. Optional.
  85. UniverseDomain string
  86. // SubjectTokenProvider is an optional token provider for OIDC/SAML
  87. // credentials. One of SubjectTokenProvider, AWSSecurityCredentialProvider
  88. // or CredentialSource must be provided. Optional.
  89. SubjectTokenProvider SubjectTokenProvider
  90. // AwsSecurityCredentialsProvider is an AWS Security Credential provider
  91. // for AWS credentials. One of SubjectTokenProvider,
  92. // AWSSecurityCredentialProvider or CredentialSource must be provided. Optional.
  93. AwsSecurityCredentialsProvider AwsSecurityCredentialsProvider
  94. // Client for token request.
  95. Client *http.Client
  96. // IsDefaultClient marks whether the client passed in is a default client that can be overriden.
  97. // This is important for X509 credentials which should create a new client if the default was used
  98. // but should respect a client explicitly passed in by the user.
  99. IsDefaultClient bool
  100. }
  101. // SubjectTokenProvider can be used to supply a subject token to exchange for a
  102. // GCP access token.
  103. type SubjectTokenProvider interface {
  104. // SubjectToken should return a valid subject token or an error.
  105. // The external account token provider does not cache the returned subject
  106. // token, so caching logic should be implemented in the provider to prevent
  107. // multiple requests for the same subject token.
  108. SubjectToken(ctx context.Context, opts *RequestOptions) (string, error)
  109. }
  110. // RequestOptions contains information about the requested subject token or AWS
  111. // security credentials from the Google external account credential.
  112. type RequestOptions struct {
  113. // Audience is the requested audience for the external account credential.
  114. Audience string
  115. // Subject token type is the requested subject token type for the external
  116. // account credential. Expected values include:
  117. // “urn:ietf:params:oauth:token-type:jwt”
  118. // “urn:ietf:params:oauth:token-type:id-token”
  119. // “urn:ietf:params:oauth:token-type:saml2”
  120. // “urn:ietf:params:aws:token-type:aws4_request”
  121. SubjectTokenType string
  122. }
  123. // AwsSecurityCredentialsProvider can be used to supply AwsSecurityCredentials
  124. // and an AWS Region to exchange for a GCP access token.
  125. type AwsSecurityCredentialsProvider interface {
  126. // AwsRegion should return the AWS region or an error.
  127. AwsRegion(ctx context.Context, opts *RequestOptions) (string, error)
  128. // GetAwsSecurityCredentials should return a valid set of
  129. // AwsSecurityCredentials or an error. The external account token provider
  130. // does not cache the returned security credentials, so caching logic should
  131. // be implemented in the provider to prevent multiple requests for the
  132. // same security credentials.
  133. AwsSecurityCredentials(ctx context.Context, opts *RequestOptions) (*AwsSecurityCredentials, error)
  134. }
  135. // AwsSecurityCredentials models AWS security credentials.
  136. type AwsSecurityCredentials struct {
  137. // AccessKeyId is the AWS Access Key ID - Required.
  138. AccessKeyID string `json:"AccessKeyID"`
  139. // SecretAccessKey is the AWS Secret Access Key - Required.
  140. SecretAccessKey string `json:"SecretAccessKey"`
  141. // SessionToken is the AWS Session token. This should be provided for
  142. // temporary AWS security credentials - Optional.
  143. SessionToken string `json:"Token"`
  144. }
  145. func (o *Options) validate() error {
  146. if o.Audience == "" {
  147. return fmt.Errorf("externalaccount: Audience must be set")
  148. }
  149. if o.SubjectTokenType == "" {
  150. return fmt.Errorf("externalaccount: Subject token type must be set")
  151. }
  152. if o.WorkforcePoolUserProject != "" {
  153. if valid := validWorkforceAudiencePattern.MatchString(o.Audience); !valid {
  154. return fmt.Errorf("externalaccount: workforce_pool_user_project should not be set for non-workforce pool credentials")
  155. }
  156. }
  157. count := 0
  158. if o.CredentialSource != nil {
  159. count++
  160. }
  161. if o.SubjectTokenProvider != nil {
  162. count++
  163. }
  164. if o.AwsSecurityCredentialsProvider != nil {
  165. count++
  166. }
  167. if count == 0 {
  168. return fmt.Errorf("externalaccount: one of CredentialSource, SubjectTokenProvider, or AwsSecurityCredentialsProvider must be set")
  169. }
  170. if count > 1 {
  171. return fmt.Errorf("externalaccount: only one of CredentialSource, SubjectTokenProvider, or AwsSecurityCredentialsProvider must be set")
  172. }
  173. return nil
  174. }
  175. // client returns the http client that should be used for the token exchange. If a non-default client
  176. // is provided, then the client configured in the options will always be returned. If a default client
  177. // is provided and the options are configured for X509 credentials, a new client will be created.
  178. func (o *Options) client() (*http.Client, error) {
  179. // If a client was provided and no override certificate config location was provided, use the provided client.
  180. if o.CredentialSource == nil || o.CredentialSource.Certificate == nil || (!o.IsDefaultClient && o.CredentialSource.Certificate.CertificateConfigLocation == "") {
  181. return o.Client, nil
  182. }
  183. // If a new client should be created, validate and use the certificate source to create a new mTLS client.
  184. cert := o.CredentialSource.Certificate
  185. if !cert.UseDefaultCertificateConfig && cert.CertificateConfigLocation == "" {
  186. return nil, errors.New("credentials: \"certificate\" object must either specify a certificate_config_location or use_default_certificate_config should be true")
  187. }
  188. if cert.UseDefaultCertificateConfig && cert.CertificateConfigLocation != "" {
  189. return nil, errors.New("credentials: \"certificate\" object cannot specify both a certificate_config_location and use_default_certificate_config=true")
  190. }
  191. return createX509Client(cert.CertificateConfigLocation)
  192. }
  193. // resolveTokenURL sets the default STS token endpoint with the configured
  194. // universe domain.
  195. func (o *Options) resolveTokenURL() {
  196. if o.TokenURL != "" {
  197. return
  198. } else if o.UniverseDomain != "" {
  199. o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, o.UniverseDomain, 1)
  200. } else {
  201. o.TokenURL = strings.Replace(defaultTokenURL, universeDomainPlaceholder, defaultUniverseDomain, 1)
  202. }
  203. }
  204. // NewTokenProvider returns a [cloud.google.com/go/auth.TokenProvider]
  205. // configured with the provided options.
  206. func NewTokenProvider(opts *Options) (auth.TokenProvider, error) {
  207. if err := opts.validate(); err != nil {
  208. return nil, err
  209. }
  210. opts.resolveTokenURL()
  211. stp, err := newSubjectTokenProvider(opts)
  212. if err != nil {
  213. return nil, err
  214. }
  215. client, err := opts.client()
  216. if err != nil {
  217. return nil, err
  218. }
  219. tp := &tokenProvider{
  220. client: client,
  221. opts: opts,
  222. stp: stp,
  223. }
  224. if opts.ServiceAccountImpersonationURL == "" {
  225. return auth.NewCachedTokenProvider(tp, nil), nil
  226. }
  227. scopes := make([]string, len(opts.Scopes))
  228. copy(scopes, opts.Scopes)
  229. // needed for impersonation
  230. tp.opts.Scopes = []string{"https://www.googleapis.com/auth/cloud-platform"}
  231. imp, err := impersonate.NewTokenProvider(&impersonate.Options{
  232. Client: client,
  233. URL: opts.ServiceAccountImpersonationURL,
  234. Scopes: scopes,
  235. Tp: auth.NewCachedTokenProvider(tp, nil),
  236. TokenLifetimeSeconds: opts.ServiceAccountImpersonationLifetimeSeconds,
  237. })
  238. if err != nil {
  239. return nil, err
  240. }
  241. return auth.NewCachedTokenProvider(imp, nil), nil
  242. }
  243. type subjectTokenProvider interface {
  244. subjectToken(ctx context.Context) (string, error)
  245. providerType() string
  246. }
  247. // tokenProvider is the provider that handles external credentials. It is used to retrieve Tokens.
  248. type tokenProvider struct {
  249. client *http.Client
  250. opts *Options
  251. stp subjectTokenProvider
  252. }
  253. func (tp *tokenProvider) Token(ctx context.Context) (*auth.Token, error) {
  254. subjectToken, err := tp.stp.subjectToken(ctx)
  255. if err != nil {
  256. return nil, err
  257. }
  258. stsRequest := &stsexchange.TokenRequest{
  259. GrantType: stsexchange.GrantType,
  260. Audience: tp.opts.Audience,
  261. Scope: tp.opts.Scopes,
  262. RequestedTokenType: stsexchange.TokenType,
  263. SubjectToken: subjectToken,
  264. SubjectTokenType: tp.opts.SubjectTokenType,
  265. }
  266. header := make(http.Header)
  267. header.Set("Content-Type", "application/x-www-form-urlencoded")
  268. header.Add("x-goog-api-client", getGoogHeaderValue(tp.opts, tp.stp))
  269. clientAuth := stsexchange.ClientAuthentication{
  270. AuthStyle: auth.StyleInHeader,
  271. ClientID: tp.opts.ClientID,
  272. ClientSecret: tp.opts.ClientSecret,
  273. }
  274. var options map[string]interface{}
  275. // Do not pass workforce_pool_user_project when client authentication is used.
  276. // The client ID is sufficient for determining the user project.
  277. if tp.opts.WorkforcePoolUserProject != "" && tp.opts.ClientID == "" {
  278. options = map[string]interface{}{
  279. "userProject": tp.opts.WorkforcePoolUserProject,
  280. }
  281. }
  282. stsResp, err := stsexchange.ExchangeToken(ctx, &stsexchange.Options{
  283. Client: tp.client,
  284. Endpoint: tp.opts.TokenURL,
  285. Request: stsRequest,
  286. Authentication: clientAuth,
  287. Headers: header,
  288. ExtraOpts: options,
  289. })
  290. if err != nil {
  291. return nil, err
  292. }
  293. tok := &auth.Token{
  294. Value: stsResp.AccessToken,
  295. Type: stsResp.TokenType,
  296. }
  297. // The RFC8693 doesn't define the explicit 0 of "expires_in" field behavior.
  298. if stsResp.ExpiresIn <= 0 {
  299. return nil, fmt.Errorf("credentials: got invalid expiry from security token service")
  300. }
  301. tok.Expiry = Now().Add(time.Duration(stsResp.ExpiresIn) * time.Second)
  302. return tok, nil
  303. }
  304. // newSubjectTokenProvider determines the type of credsfile.CredentialSource needed to create a
  305. // subjectTokenProvider
  306. func newSubjectTokenProvider(o *Options) (subjectTokenProvider, error) {
  307. reqOpts := &RequestOptions{Audience: o.Audience, SubjectTokenType: o.SubjectTokenType}
  308. if o.AwsSecurityCredentialsProvider != nil {
  309. return &awsSubjectProvider{
  310. securityCredentialsProvider: o.AwsSecurityCredentialsProvider,
  311. TargetResource: o.Audience,
  312. reqOpts: reqOpts,
  313. }, nil
  314. } else if o.SubjectTokenProvider != nil {
  315. return &programmaticProvider{stp: o.SubjectTokenProvider, opts: reqOpts}, nil
  316. } else if len(o.CredentialSource.EnvironmentID) > 3 && o.CredentialSource.EnvironmentID[:3] == "aws" {
  317. if awsVersion, err := strconv.Atoi(o.CredentialSource.EnvironmentID[3:]); err == nil {
  318. if awsVersion != 1 {
  319. return nil, fmt.Errorf("credentials: aws version '%d' is not supported in the current build", awsVersion)
  320. }
  321. awsProvider := &awsSubjectProvider{
  322. EnvironmentID: o.CredentialSource.EnvironmentID,
  323. RegionURL: o.CredentialSource.RegionURL,
  324. RegionalCredVerificationURL: o.CredentialSource.RegionalCredVerificationURL,
  325. CredVerificationURL: o.CredentialSource.URL,
  326. TargetResource: o.Audience,
  327. Client: o.Client,
  328. }
  329. if o.CredentialSource.IMDSv2SessionTokenURL != "" {
  330. awsProvider.IMDSv2SessionTokenURL = o.CredentialSource.IMDSv2SessionTokenURL
  331. }
  332. return awsProvider, nil
  333. }
  334. } else if o.CredentialSource.File != "" {
  335. return &fileSubjectProvider{File: o.CredentialSource.File, Format: o.CredentialSource.Format}, nil
  336. } else if o.CredentialSource.URL != "" {
  337. return &urlSubjectProvider{URL: o.CredentialSource.URL, Headers: o.CredentialSource.Headers, Format: o.CredentialSource.Format, Client: o.Client}, nil
  338. } else if o.CredentialSource.Executable != nil {
  339. ec := o.CredentialSource.Executable
  340. if ec.Command == "" {
  341. return nil, errors.New("credentials: missing `command` field — executable command must be provided")
  342. }
  343. execProvider := &executableSubjectProvider{}
  344. execProvider.Command = ec.Command
  345. if ec.TimeoutMillis == 0 {
  346. execProvider.Timeout = executableDefaultTimeout
  347. } else {
  348. execProvider.Timeout = time.Duration(ec.TimeoutMillis) * time.Millisecond
  349. if execProvider.Timeout < timeoutMinimum || execProvider.Timeout > timeoutMaximum {
  350. return nil, fmt.Errorf("credentials: invalid `timeout_millis` field — executable timeout must be between %v and %v seconds", timeoutMinimum.Seconds(), timeoutMaximum.Seconds())
  351. }
  352. }
  353. execProvider.OutputFile = ec.OutputFile
  354. execProvider.client = o.Client
  355. execProvider.opts = o
  356. execProvider.env = runtimeEnvironment{}
  357. return execProvider, nil
  358. } else if o.CredentialSource.Certificate != nil {
  359. cert := o.CredentialSource.Certificate
  360. if !cert.UseDefaultCertificateConfig && cert.CertificateConfigLocation == "" {
  361. return nil, errors.New("credentials: \"certificate\" object must either specify a certificate_config_location or use_default_certificate_config should be true")
  362. }
  363. if cert.UseDefaultCertificateConfig && cert.CertificateConfigLocation != "" {
  364. return nil, errors.New("credentials: \"certificate\" object cannot specify both a certificate_config_location and use_default_certificate_config=true")
  365. }
  366. return &x509Provider{}, nil
  367. }
  368. return nil, errors.New("credentials: unable to parse credential source")
  369. }
  370. func getGoogHeaderValue(conf *Options, p subjectTokenProvider) string {
  371. return fmt.Sprintf("gl-go/%s auth/%s google-byoid-sdk source/%s sa-impersonation/%t config-lifetime/%t",
  372. goVersion(),
  373. "unknown",
  374. p.providerType(),
  375. conf.ServiceAccountImpersonationURL != "",
  376. conf.ServiceAccountImpersonationLifetimeSeconds != 0)
  377. }