option.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright 2017 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package option contains options for Google API clients.
  5. package option
  6. import (
  7. "crypto/tls"
  8. "net/http"
  9. "golang.org/x/oauth2"
  10. "golang.org/x/oauth2/google"
  11. "google.golang.org/api/internal"
  12. "google.golang.org/api/internal/impersonate"
  13. "google.golang.org/grpc"
  14. )
  15. // A ClientOption is an option for a Google API client.
  16. type ClientOption interface {
  17. Apply(*internal.DialSettings)
  18. }
  19. // WithTokenSource returns a ClientOption that specifies an OAuth2 token
  20. // source to be used as the basis for authentication.
  21. func WithTokenSource(s oauth2.TokenSource) ClientOption {
  22. return withTokenSource{s}
  23. }
  24. type withTokenSource struct{ ts oauth2.TokenSource }
  25. func (w withTokenSource) Apply(o *internal.DialSettings) {
  26. o.TokenSource = w.ts
  27. }
  28. type withCredFile string
  29. func (w withCredFile) Apply(o *internal.DialSettings) {
  30. o.CredentialsFile = string(w)
  31. }
  32. // WithCredentialsFile returns a ClientOption that authenticates
  33. // API calls with the given service account or refresh token JSON
  34. // credentials file.
  35. func WithCredentialsFile(filename string) ClientOption {
  36. return withCredFile(filename)
  37. }
  38. // WithServiceAccountFile returns a ClientOption that uses a Google service
  39. // account credentials file to authenticate.
  40. //
  41. // Deprecated: Use WithCredentialsFile instead.
  42. func WithServiceAccountFile(filename string) ClientOption {
  43. return WithCredentialsFile(filename)
  44. }
  45. // WithCredentialsJSON returns a ClientOption that authenticates
  46. // API calls with the given service account or refresh token JSON
  47. // credentials.
  48. func WithCredentialsJSON(p []byte) ClientOption {
  49. return withCredentialsJSON(p)
  50. }
  51. type withCredentialsJSON []byte
  52. func (w withCredentialsJSON) Apply(o *internal.DialSettings) {
  53. o.CredentialsJSON = make([]byte, len(w))
  54. copy(o.CredentialsJSON, w)
  55. }
  56. // WithEndpoint returns a ClientOption that overrides the default endpoint
  57. // to be used for a service.
  58. func WithEndpoint(url string) ClientOption {
  59. return withEndpoint(url)
  60. }
  61. type withEndpoint string
  62. func (w withEndpoint) Apply(o *internal.DialSettings) {
  63. o.Endpoint = string(w)
  64. }
  65. // WithScopes returns a ClientOption that overrides the default OAuth2 scopes
  66. // to be used for a service.
  67. //
  68. // If both WithScopes and WithTokenSource are used, scope settings from the
  69. // token source will be used instead.
  70. func WithScopes(scope ...string) ClientOption {
  71. return withScopes(scope)
  72. }
  73. type withScopes []string
  74. func (w withScopes) Apply(o *internal.DialSettings) {
  75. o.Scopes = make([]string, len(w))
  76. copy(o.Scopes, w)
  77. }
  78. // WithUserAgent returns a ClientOption that sets the User-Agent.
  79. func WithUserAgent(ua string) ClientOption {
  80. return withUA(ua)
  81. }
  82. type withUA string
  83. func (w withUA) Apply(o *internal.DialSettings) { o.UserAgent = string(w) }
  84. // WithHTTPClient returns a ClientOption that specifies the HTTP client to use
  85. // as the basis of communications. This option may only be used with services
  86. // that support HTTP as their communication transport. When used, the
  87. // WithHTTPClient option takes precedent over all other supplied options.
  88. func WithHTTPClient(client *http.Client) ClientOption {
  89. return withHTTPClient{client}
  90. }
  91. type withHTTPClient struct{ client *http.Client }
  92. func (w withHTTPClient) Apply(o *internal.DialSettings) {
  93. o.HTTPClient = w.client
  94. }
  95. // WithGRPCConn returns a ClientOption that specifies the gRPC client
  96. // connection to use as the basis of communications. This option may only be
  97. // used with services that support gRPC as their communication transport. When
  98. // used, the WithGRPCConn option takes precedent over all other supplied
  99. // options.
  100. func WithGRPCConn(conn *grpc.ClientConn) ClientOption {
  101. return withGRPCConn{conn}
  102. }
  103. type withGRPCConn struct{ conn *grpc.ClientConn }
  104. func (w withGRPCConn) Apply(o *internal.DialSettings) {
  105. o.GRPCConn = w.conn
  106. }
  107. // WithGRPCDialOption returns a ClientOption that appends a new grpc.DialOption
  108. // to an underlying gRPC dial. It does not work with WithGRPCConn.
  109. func WithGRPCDialOption(opt grpc.DialOption) ClientOption {
  110. return withGRPCDialOption{opt}
  111. }
  112. type withGRPCDialOption struct{ opt grpc.DialOption }
  113. func (w withGRPCDialOption) Apply(o *internal.DialSettings) {
  114. o.GRPCDialOpts = append(o.GRPCDialOpts, w.opt)
  115. }
  116. // WithGRPCConnectionPool returns a ClientOption that creates a pool of gRPC
  117. // connections that requests will be balanced between.
  118. func WithGRPCConnectionPool(size int) ClientOption {
  119. return withGRPCConnectionPool(size)
  120. }
  121. type withGRPCConnectionPool int
  122. func (w withGRPCConnectionPool) Apply(o *internal.DialSettings) {
  123. o.GRPCConnPoolSize = int(w)
  124. }
  125. // WithAPIKey returns a ClientOption that specifies an API key to be used
  126. // as the basis for authentication.
  127. //
  128. // API Keys can only be used for JSON-over-HTTP APIs, including those under
  129. // the import path google.golang.org/api/....
  130. func WithAPIKey(apiKey string) ClientOption {
  131. return withAPIKey(apiKey)
  132. }
  133. type withAPIKey string
  134. func (w withAPIKey) Apply(o *internal.DialSettings) { o.APIKey = string(w) }
  135. // WithAudiences returns a ClientOption that specifies an audience to be used
  136. // as the audience field ("aud") for the JWT token authentication.
  137. func WithAudiences(audience ...string) ClientOption {
  138. return withAudiences(audience)
  139. }
  140. type withAudiences []string
  141. func (w withAudiences) Apply(o *internal.DialSettings) {
  142. o.Audiences = make([]string, len(w))
  143. copy(o.Audiences, w)
  144. }
  145. // WithoutAuthentication returns a ClientOption that specifies that no
  146. // authentication should be used. It is suitable only for testing and for
  147. // accessing public resources, like public Google Cloud Storage buckets.
  148. // It is an error to provide both WithoutAuthentication and any of WithAPIKey,
  149. // WithTokenSource, WithCredentialsFile or WithServiceAccountFile.
  150. func WithoutAuthentication() ClientOption {
  151. return withoutAuthentication{}
  152. }
  153. type withoutAuthentication struct{}
  154. func (w withoutAuthentication) Apply(o *internal.DialSettings) { o.NoAuth = true }
  155. // WithQuotaProject returns a ClientOption that specifies the project used
  156. // for quota and billing purposes.
  157. //
  158. // For more information please read:
  159. // https://cloud.google.com/apis/docs/system-parameters
  160. func WithQuotaProject(quotaProject string) ClientOption {
  161. return withQuotaProject(quotaProject)
  162. }
  163. type withQuotaProject string
  164. func (w withQuotaProject) Apply(o *internal.DialSettings) {
  165. o.QuotaProject = string(w)
  166. }
  167. // WithRequestReason returns a ClientOption that specifies a reason for
  168. // making the request, which is intended to be recorded in audit logging.
  169. // An example reason would be a support-case ticket number.
  170. //
  171. // For more information please read:
  172. // https://cloud.google.com/apis/docs/system-parameters
  173. func WithRequestReason(requestReason string) ClientOption {
  174. return withRequestReason(requestReason)
  175. }
  176. type withRequestReason string
  177. func (w withRequestReason) Apply(o *internal.DialSettings) {
  178. o.RequestReason = string(w)
  179. }
  180. // WithTelemetryDisabled returns a ClientOption that disables default telemetry (OpenCensus)
  181. // settings on gRPC and HTTP clients.
  182. // An example reason would be to bind custom telemetry that overrides the defaults.
  183. func WithTelemetryDisabled() ClientOption {
  184. return withTelemetryDisabled{}
  185. }
  186. type withTelemetryDisabled struct{}
  187. func (w withTelemetryDisabled) Apply(o *internal.DialSettings) {
  188. o.TelemetryDisabled = true
  189. }
  190. // ClientCertSource is a function that returns a TLS client certificate to be used
  191. // when opening TLS connections.
  192. //
  193. // It follows the same semantics as crypto/tls.Config.GetClientCertificate.
  194. //
  195. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  196. type ClientCertSource = func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
  197. // WithClientCertSource returns a ClientOption that specifies a
  198. // callback function for obtaining a TLS client certificate.
  199. //
  200. // This option is used for supporting mTLS authentication, where the
  201. // server validates the client certifcate when establishing a connection.
  202. //
  203. // The callback function will be invoked whenever the server requests a
  204. // certificate from the client. Implementations of the callback function
  205. // should try to ensure that a valid certificate can be repeatedly returned
  206. // on demand for the entire life cycle of the transport client. If a nil
  207. // Certificate is returned (i.e. no Certificate can be obtained), an error
  208. // should be returned.
  209. //
  210. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  211. func WithClientCertSource(s ClientCertSource) ClientOption {
  212. return withClientCertSource{s}
  213. }
  214. type withClientCertSource struct{ s ClientCertSource }
  215. func (w withClientCertSource) Apply(o *internal.DialSettings) {
  216. o.ClientCertSource = w.s
  217. }
  218. // ImpersonateCredentials returns a ClientOption that will impersonate the
  219. // target service account.
  220. //
  221. // In order to impersonate the target service account
  222. // the base service account must have the Service Account Token Creator role,
  223. // roles/iam.serviceAccountTokenCreator, on the target service account.
  224. // See https://cloud.google.com/iam/docs/understanding-service-accounts.
  225. //
  226. // Optionally, delegates can be used during impersonation if the base service
  227. // account lacks the token creator role on the target. When using delegates,
  228. // each service account must be granted roles/iam.serviceAccountTokenCreator
  229. // on the next service account in the chain.
  230. //
  231. // For example, if a base service account of SA1 is trying to impersonate target
  232. // service account SA2 while using delegate service accounts DSA1 and DSA2,
  233. // the following must be true:
  234. //
  235. // 1. Base service account SA1 has roles/iam.serviceAccountTokenCreator on
  236. // DSA1.
  237. // 2. DSA1 has roles/iam.serviceAccountTokenCreator on DSA2.
  238. // 3. DSA2 has roles/iam.serviceAccountTokenCreator on target SA2.
  239. //
  240. // The resulting impersonated credential will either have the default scopes of
  241. // the client being instantiating or the scopes from WithScopes if provided.
  242. // Scopes are required for creating impersonated credentials, so if this option
  243. // is used while not using a NewClient/NewService function, WithScopes must also
  244. // be explicitly passed in as well.
  245. //
  246. // If the base credential is an authorized user and not a service account, or if
  247. // the option WithQuotaProject is set, the target service account must have a
  248. // role that grants the serviceusage.services.use permission such as
  249. // roles/serviceusage.serviceUsageConsumer.
  250. //
  251. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  252. //
  253. // Deprecated: This option has been replaced by `impersonate` package:
  254. // `google.golang.org/api/impersonate`. Please use the `impersonate` package
  255. // instead with the WithTokenSource option.
  256. func ImpersonateCredentials(target string, delegates ...string) ClientOption {
  257. return impersonateServiceAccount{
  258. target: target,
  259. delegates: delegates,
  260. }
  261. }
  262. type impersonateServiceAccount struct {
  263. target string
  264. delegates []string
  265. }
  266. func (i impersonateServiceAccount) Apply(o *internal.DialSettings) {
  267. o.ImpersonationConfig = &impersonate.Config{
  268. Target: i.target,
  269. }
  270. o.ImpersonationConfig.Delegates = make([]string, len(i.delegates))
  271. copy(o.ImpersonationConfig.Delegates, i.delegates)
  272. }
  273. type withCreds google.Credentials
  274. func (w *withCreds) Apply(o *internal.DialSettings) {
  275. o.Credentials = (*google.Credentials)(w)
  276. }
  277. // WithCredentials returns a ClientOption that authenticates API calls.
  278. func WithCredentials(creds *google.Credentials) ClientOption {
  279. return (*withCreds)(creds)
  280. }