Skip to content

Commit 4c8fb8d

Browse files
Christian DoucetteChristian Doucette
authored andcommitted
Initial commit (functionality but not tests)
1 parent 07ee06e commit 4c8fb8d

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

internal/client/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ func getTokenFromCreds(services *disco.Disco, hostname svchost.Hostname) string
8181
//
8282
// Internally, this function caches configured clients using the specified
8383
// parameters
84-
func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, error) {
85-
config, err := configure(tfeHost, token, insecure)
84+
func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, bool, error) {
85+
config, sendCredentialDeprecationWarning, err := configure(tfeHost, token, insecure)
8686
if err != nil {
87-
return nil, err
87+
return nil, sendCredentialDeprecationWarning, err
8888
}
8989

9090
clientCache.Lock()
@@ -93,13 +93,13 @@ func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, error) {
9393
// Try to retrieve the client from cache
9494
cached := clientCache.GetByConfig(config)
9595
if cached != nil {
96-
return cached, nil
96+
return cached, sendCredentialDeprecationWarning, nil
9797
}
9898

9999
// Discover the Terraform Enterprise address.
100100
host, err := config.Services.Discover(config.TFEHost)
101101
if err != nil {
102-
return nil, fmt.Errorf("failed to create client: %w", err)
102+
return nil, sendCredentialDeprecationWarning, fmt.Errorf("failed to create client: %w", err)
103103
}
104104

105105
// Get the full Terraform Enterprise service address.
@@ -109,7 +109,7 @@ func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, error) {
109109
service, err := host.ServiceURL(tfeServiceID)
110110
target := &disco.ErrVersionNotSupported{}
111111
if err != nil && !errors.As(err, &target) {
112-
return nil, fmt.Errorf("failed to create client: %w", err)
112+
return nil, sendCredentialDeprecationWarning, fmt.Errorf("failed to create client: %w", err)
113113
}
114114

115115
// If discoErr is nil we save the first error. When multiple services
@@ -133,15 +133,15 @@ func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, error) {
133133
// First check any constraints we might have received.
134134
if constraints != nil {
135135
if err := CheckConstraints(constraints); err != nil {
136-
return nil, err
136+
return nil, sendCredentialDeprecationWarning, err
137137
}
138138
}
139139
}
140140

141141
// When we don't have any constraints errors, also check for discovery
142142
// errors before we continue.
143143
if discoErr != nil {
144-
return nil, discoErr
144+
return nil, sendCredentialDeprecationWarning, discoErr
145145
}
146146

147147
// Create a new TFE client.
@@ -151,13 +151,13 @@ func GetClient(tfeHost, token string, insecure bool) (*tfe.Client, error) {
151151
HTTPClient: config.HTTPClient,
152152
})
153153
if err != nil {
154-
return nil, fmt.Errorf("failed to create client: %w", err)
154+
return nil, sendCredentialDeprecationWarning, fmt.Errorf("failed to create client: %w", err)
155155
}
156156

157157
client.RetryServerErrors(true)
158158
clientCache.Set(client, config)
159159

160-
return client, nil
160+
return client, sendCredentialDeprecationWarning, nil
161161
}
162162

163163
// CheckConstraints checks service version constrains against our own

internal/client/config.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,17 @@ func credentialsSource(credentials CredentialsMap) auth.CredentialsSource {
165165
return creds
166166
}
167167

168+
// Using presence of TFC_AGENT_VERSION to determine if this provider is running on HCP Terraform / enterprise
169+
func providerRunningInCloud() bool {
170+
_, present := os.LookupEnv("TFC_AGENT_VERSION")
171+
return present
172+
}
173+
168174
// configure accepts the provider-level configuration values and creates a
169175
// clientConfiguration using fallback values from the environment or CLI configuration.
170-
func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, error) {
176+
func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, bool, error) {
177+
sendCredentialDeprecationWarning := false
178+
171179
if tfeHost == "" {
172180
if os.Getenv("TFE_HOSTNAME") != "" {
173181
tfeHost = os.Getenv("TFE_HOSTNAME")
@@ -186,7 +194,7 @@ func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, erro
186194
v := os.Getenv("TFE_SSL_SKIP_VERIFY")
187195
insecure, err = strconv.ParseBool(v)
188196
if err != nil {
189-
return nil, fmt.Errorf("TFE_SSL_SKIP_VERIFY has unrecognized value %q", v)
197+
return nil, sendCredentialDeprecationWarning, fmt.Errorf("TFE_SSL_SKIP_VERIFY has unrecognized value %q", v)
190198
}
191199
}
192200

@@ -198,7 +206,7 @@ func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, erro
198206
// Parse the hostname for comparison,
199207
hostname, err := svchost.ForComparison(tfeHost)
200208
if err != nil {
201-
return nil, fmt.Errorf("invalid hostname %q: %w", tfeHost, err)
209+
return nil, sendCredentialDeprecationWarning, fmt.Errorf("invalid hostname %q: %w", tfeHost, err)
202210
}
203211

204212
httpClient := tfe.DefaultConfig().HTTPClient
@@ -232,17 +240,19 @@ func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, erro
232240

233241
// If a token wasn't set in the provider configuration block, try and fetch it
234242
// from the environment or from Terraform's CLI configuration or configured credential helper.
243+
235244
if token == "" {
236245
if os.Getenv("TFE_TOKEN") != "" {
237246
token = getTokenFromEnv()
238247
} else {
248+
sendCredentialDeprecationWarning = providerRunningInCloud()
239249
token = getTokenFromCreds(services, hostname)
240250
}
241251
}
242252

243253
// If we still don't have a token at this point, we return an error.
244254
if token == "" {
245-
return nil, ErrMissingAuthToken
255+
return nil, sendCredentialDeprecationWarning, ErrMissingAuthToken
246256
}
247257

248258
return &ClientConfiguration{
@@ -251,5 +261,5 @@ func configure(tfeHost, token string, insecure bool) (*ClientConfiguration, erro
251261
TFEHost: hostname,
252262
Token: token,
253263
Insecure: insecure,
254-
}, nil
264+
}, sendCredentialDeprecationWarning, nil
255265
}

internal/provider/provider.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,30 @@ func configure() schema.ConfigureContextFunc {
153153
providerOrganization = os.Getenv("TFE_ORGANIZATION")
154154
}
155155

156-
tfeClient, err := configureClient(rd)
156+
tfeClient, sendCredentialDeprecationWarning, err := configureClient(rd)
157157
if err != nil {
158158
return nil, diag.FromErr(err)
159159
}
160160

161+
var diagnosticWarnings diag.Diagnostics = nil
162+
if sendCredentialDeprecationWarning {
163+
diagnosticWarnings = diag.Diagnostics{
164+
diag.Diagnostic{
165+
Severity: diag.Warning,
166+
Summary: "Authentication method invalid for TFE Provider with HCP Terraform and Terraform Enterprise",
167+
Detail: "Use a TFE_TOKEN variable in the workspace or the token argument for the provider. This authentication method will be deprecated in a future version.",
168+
},
169+
}
170+
}
171+
161172
return ConfiguredClient{
162173
tfeClient,
163174
providerOrganization,
164-
}, nil
175+
}, diagnosticWarnings
165176
}
166177
}
167178

168-
func configureClient(d *schema.ResourceData) (*tfe.Client, error) {
179+
func configureClient(d *schema.ResourceData) (*tfe.Client, bool, error) {
169180
hostname := d.Get("hostname").(string)
170181
token := d.Get("token").(string)
171182
insecure := d.Get("ssl_skip_verify").(bool)

internal/provider/provider_next.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,17 @@ func (p *frameworkProvider) Configure(ctx context.Context, req provider.Configur
111111
}
112112
}
113113

114-
tfeClient, err := client.GetClient(data.Hostname.ValueString(), data.Token.ValueString(), data.SSLSkipVerify.ValueBool())
114+
tfeClient, sendCredentialDeprecationWarning, err := client.GetClient(data.Hostname.ValueString(), data.Token.ValueString(), data.SSLSkipVerify.ValueBool())
115115

116116
if err != nil {
117117
res.Diagnostics.AddError("Failed to initialize HTTP client", err.Error())
118118
return
119119
}
120120

121+
if sendCredentialDeprecationWarning {
122+
res.Diagnostics.AddWarning("Authentication method invalid for TFE Provider with HCP Terraform and Terraform Enterprise", "Use a TFE_TOKEN variable in the workspace or the token argument for the provider. This authentication method will be deprecated in a future version.")
123+
}
124+
121125
configuredClient := ConfiguredClient{
122126
Client: tfeClient,
123127
Organization: data.Organization.ValueString(),

0 commit comments

Comments
 (0)