Debian Codegraph @

gcp/gcp.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
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 // https://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
15 // Package gcp provides fundamental Wire providers and types for Google Cloud Platform (GCP).
16 package gcp // import "gocloud.dev/gcp"
17
18 import (
19 "context"
20 "errors"
21 "net/http"
22
23 "github.com/google/wire"
24 "golang.org/x/oauth2"
25 "golang.org/x/oauth2/google"
26 )
27
28 // DefaultIdentity is a Wire provider set that provides the project ID
29 // and token source from Application Default Credentials (ADC).
30 var DefaultIdentity = wire.NewSet(
31 CredentialsTokenSource,
32 DefaultCredentials,
33 DefaultProjectID)
34
35 // ProjectID is a GCP Project ID.
36 type ProjectID string
37
38 // TokenSource wraps a GCP token source that provides Cloud-Platform-
39 // scoped tokens.
40 type TokenSource oauth2.TokenSource
41
42 // HTTPClient is an HTTP client that makes requests authenticated with Cloud-
43 // Platform-scoped authentication tokens.
44 type HTTPClient struct {
45 http.Client
46 }
47
48 // NewAnonymousHTTPClient creates a new anonymous HTTP client.
49 func NewAnonymousHTTPClient(transport http.RoundTripper) *HTTPClient {
50 return &HTTPClient{
51 Client: http.Client{
52 Transport: transport,
53 },
54 }
55 }
56
57 // NewHTTPClient creates a new authenticated HTTP client.
58 func NewHTTPClient(transport http.RoundTripper, ts TokenSource) (*HTTPClient, error) {
59 if ts == nil {
60 return nil, errors.New("gcp: no credentials available")
61 }
62 return &HTTPClient{
63 Client: http.Client{
64 Transport: &oauth2.Transport{
65 Base: transport,
66 Source: ts,
67 },
68 },
69 }, nil
70 }
71
72 // DefaultTransport returns http.DefaultTransport.
73 func DefaultTransport() http.RoundTripper {
74 return http.DefaultTransport
75 }
76
77 // DefaultCredentials obtains the default GCP credentials with Cloud Platform
78 // scope.
79 func DefaultCredentials(ctx context.Context) (*google.Credentials, error) {
80 adc, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
81 if err != nil {
82 return nil, err
83 }
84 return adc, nil
85 }
86
87 // DefaultCredentialsWithParams obtains the default GCP credentials with the
88 // specified parameters. The Scopes field in params will be overridden with
89 // Cloud Platform scope if not set.
90 func DefaultCredentialsWithParams(ctx context.Context, params google.CredentialsParams) (*google.Credentials, error) {
91 if len(params.Scopes) == 0 {
92 params.Scopes = []string{"https://www.googleapis.com/auth/cloud-platform"}
93 }
94 adc, err := google.FindDefaultCredentialsWithParams(ctx, params)
95 if err != nil {
96 return nil, err
97 }
98 return adc, nil
99 }
100
101 // CredentialsTokenSource extracts the token source from GCP credentials.
102 func CredentialsTokenSource(creds *google.Credentials) TokenSource {
103 if creds == nil {
104 return nil
105 }
106 return TokenSource(creds.TokenSource)
107 }
108
109 // DefaultProjectID obtains the project ID from the default GCP credentials.
110 func DefaultProjectID(creds *google.Credentials) (ProjectID, error) {
111 if creds == nil {
112 return "", errors.New("gcp: no project found in credentials")
113 }
114 return ProjectID(creds.ProjectID), nil
115 }