This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprovider.go
158 lines (133 loc) · 4.58 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2019 Seth Vargo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"context"
"fmt"
"log"
"strings"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"github.com/GoogleCloudPlatform/berglas/pkg/berglas"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/sethvargo/terraform-provider-berglas/internal/pathorcontents"
)
const (
cloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform"
)
func init() {
schema.DescriptionKind = schema.StringMarkdown
}
func New(version string) func() *schema.Provider {
return func() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"credentials": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_APPLICATION_CREDENTIALS",
"GOOGLE_CREDENTIALS",
"GOOGLE_CLOUD_KEYFILE_JSON",
"GCLOUD_KEYFILE_JSON",
}, nil),
Description: strings.TrimSpace(`
JSON credentials with which to authenticate against the API. This can be set to
the raw credential contents or it can be set to a file path on disk which
contains the file contents.
`),
ConflictsWith: []string{"access_token"},
},
"access_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"GOOGLE_OAUTH_ACCESS_TOKEN",
}, nil),
Description: strings.TrimSpace(`
OAuth2 access token to use for communicating with Google APIs.
`),
ConflictsWith: []string{"credentials"},
},
},
DataSourcesMap: map[string]*schema.Resource{
"berglas_secret": dataSourceBerglasSecret(),
},
ResourcesMap: map[string]*schema.Resource{
"berglas_secret": resourceBerglasSecret(),
},
}
// Meta, but we have to pass the provider into itself
p.ConfigureContextFunc = providerConfigure(version, p)
return p
}
}
// providerConfigure configures the provider
func providerConfigure(version string, p *schema.Provider) schema.ConfigureContextFunc {
return func(_ context.Context, d *schema.ResourceData) (any, diag.Diagnostics) {
accessToken := d.Get("access_token").(string)
credentials := d.Get("credentials").(string)
// Note that we explicitly use context.Background() instead of the provided
// context because we want to give the client a chance to finish before
// cleanup.
tokenSource, err := tokenSource(context.Background(), accessToken, credentials)
if err != nil {
return nil, diag.FromErr(fmt.Errorf("failed to configure provider: %w", err))
}
client, err := berglas.New(context.Background(), option.WithTokenSource(tokenSource))
if err != nil {
return nil, diag.FromErr(fmt.Errorf("failed to setup berglas: %w", err))
}
config := &config{
client: client,
}
return config, nil
}
}
// tokenSource returns the best token source for the given environment.
func tokenSource(ctx context.Context, accessToken, credentials string) (oauth2.TokenSource, error) {
// Try access token first
if accessToken != "" {
log.Printf("[INFO] authenticating via access_token")
contents, _, err := pathorcontents.Read(accessToken)
if err != nil {
return nil, fmt.Errorf("failed to load access token: %w", err)
}
return oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: contents,
}), nil
}
// Then credentials
if credentials != "" {
log.Printf("[INFO] authenticating via credentials")
contents, _, err := pathorcontents.Read(credentials)
if err != nil {
return nil, fmt.Errorf("failed to load credentials: %w", err)
}
creds, err := google.CredentialsFromJSON(ctx, []byte(contents), cloudPlatformScope)
if err != nil {
return nil, fmt.Errorf("failed to parse credentials: %w", err)
}
return creds.TokenSource, nil
}
// Fallback to default credentials
log.Printf("[INFO] authenticating via default credentials")
source, err := google.DefaultTokenSource(ctx, cloudPlatformScope)
if err != nil {
return nil, fmt.Errorf("failed to get default credentials: %w", err)
}
return source, nil
}