Skip to content

Commit

Permalink
Add tls_server_name provider options. (#1638)
Browse files Browse the repository at this point in the history
* Add `tls_server_name` provider options.

* Restore accidentally removed `client_certificate` parameter.

* Fixed description

* add changelog-entry

---------

Co-authored-by: Mauricio Alvarez Leon <[email protected]>
Co-authored-by: BBBmau <[email protected]>
  • Loading branch information
3 people committed Jun 22, 2023
1 parent e9fab70 commit 39a6b40
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/1638.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
`kubernetes/provider.go`: Add `tls_server_name` kubernetes provider options.
```
13 changes: 11 additions & 2 deletions kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func Provider() *schema.Provider {
DefaultFunc: schema.EnvDefaultFunc("KUBE_INSECURE", false),
Description: "Whether server should be accessed without verifying the TLS certificate.",
},
"tls_server_name": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KUBE_TLS_SERVER_NAME", ""),
Description: "Server name passed to the server for SNI and is used in the client to check server certificates against.",
},
"client_certificate": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -524,7 +530,7 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
authInfo, authInfoOk := d.GetOk("config_context_auth_info")
cluster, clusterOk := d.GetOk("config_context_cluster")
if ctxOk || authInfoOk || clusterOk {
ctxSuffix = "; overriden context"
ctxSuffix = "; overridden context"
if ctxOk {
overrides.CurrentContext = kubectx.(string)
ctxSuffix += fmt.Sprintf("; config ctx: %s", overrides.CurrentContext)
Expand All @@ -540,14 +546,17 @@ func initializeConfiguration(d *schema.ResourceData) (*restclient.Config, error)
overrides.Context.Cluster = cluster.(string)
ctxSuffix += fmt.Sprintf("; cluster: %s", overrides.Context.Cluster)
}
log.Printf("[DEBUG] Using overidden context: %#v", overrides.Context)
log.Printf("[DEBUG] Using overridden context: %#v", overrides.Context)
}
}

// Overriding with static configuration
if v, ok := d.GetOk("insecure"); ok {
overrides.ClusterInfo.InsecureSkipTLSVerify = v.(bool)
}
if v, ok := d.GetOk("tls_server_name"); ok {
overrides.ClusterInfo.TLSServerName = v.(string)
}
if v, ok := d.GetOk("cluster_ca_certificate"); ok {
overrides.ClusterInfo.CertificateAuthorityData = bytes.NewBufferString(v.(string)).Bytes()
}
Expand Down
3 changes: 3 additions & 0 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func unsetEnv(t *testing.T) func() {
"KUBE_CLIENT_KEY_DATA": e.ClientKeyData,
"KUBE_CLUSTER_CA_CERT_DATA": e.ClusterCACertData,
"KUBE_INSECURE": e.Insecure,
"KUBE_TLS_SERVER_NAME": e.TLSServerName,
"KUBE_TOKEN": e.Token,
}

Expand Down Expand Up @@ -150,6 +151,7 @@ func getEnv() *currentEnv {
ClientKeyData: os.Getenv("KUBE_CLIENT_KEY_DATA"),
ClusterCACertData: os.Getenv("KUBE_CLUSTER_CA_CERT_DATA"),
Insecure: os.Getenv("KUBE_INSECURE"),
TLSServerName: os.Getenv("KUBE_TLS_SERVER_NAME"),
Token: os.Getenv("KUBE_TOKEN"),
}
if v := os.Getenv("KUBE_CONFIG_PATH"); v != "" {
Expand Down Expand Up @@ -449,5 +451,6 @@ type currentEnv struct {
ClientKeyData string
ClusterCACertData string
Insecure string
TLSServerName string
Token string
}
20 changes: 20 additions & 0 deletions manifest/provider/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ func (s *RawProviderServer) ConfigureProvider(ctx context.Context, req *tfprotov
}
overrides.ClusterInfo.InsecureSkipTLSVerify = insecure

// Handle 'tls_server_name' attribute
//
var tlsServerName string
if !providerConfig["tls_server_name"].IsNull() && providerConfig["tls_server_name"].IsKnown() {
err = providerConfig["tls_server_name"].As(&tlsServerName)
if err != nil {
// invalid attribute type - this shouldn't happen, bail out for now
response.Diagnostics = append(response.Diagnostics, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Provider configuration: failed to assert type of 'tls_server_name' value",
Detail: err.Error(),
})
return response, nil
}
overrides.ClusterInfo.TLSServerName = tlsServerName
}
if tlsServerName, ok := os.LookupEnv("KUBE_TLS_SERVER_NAME"); ok && tlsServerName != "" {
overrides.ClusterInfo.TLSServerName = tlsServerName
}

hasCA := len(overrides.ClusterInfo.CertificateAuthorityData) != 0
hasCert := len(overrides.AuthInfo.ClientCertificateData) != 0
defaultTLS := hasCA || hasCert || overrides.ClusterInfo.InsecureSkipTLSVerify
Expand Down
11 changes: 11 additions & 0 deletions manifest/provider/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ func GetProviderConfigSchema() *tfprotov5.Schema {
DescriptionKind: 0,
Deprecated: false,
},
{
Name: "tls_server_name",
Type: tftypes.String,
Description: "Server name passed to the server for SNI and is used in the client to check server certificates against.",
Required: false,
Optional: true,
Computed: false,
Sensitive: false,
DescriptionKind: 0,
Deprecated: false,
},
{
Name: "client_certificate",
Type: tftypes.String,
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ The following arguments are supported:
* `username` - (Optional) The username to use for HTTP basic authentication when accessing the Kubernetes API. Can be sourced from `KUBE_USER`.
* `password` - (Optional) The password to use for HTTP basic authentication when accessing the Kubernetes API. Can be sourced from `KUBE_PASSWORD`.
* `insecure` - (Optional) Whether the server should be accessed without verifying the TLS certificate. Can be sourced from `KUBE_INSECURE`. Defaults to `false`.
* `tls_server_name` - (Optional) Server name passed to the server for SNI and is used in the client to check server certificates against. Can be sourced from `KUBE_TLS_SERVER_NAME`.
* `client_certificate` - (Optional) PEM-encoded client certificate for TLS authentication. Can be sourced from `KUBE_CLIENT_CERT_DATA`.
* `client_key` - (Optional) PEM-encoded client certificate key for TLS authentication. Can be sourced from `KUBE_CLIENT_KEY_DATA`.
* `cluster_ca_certificate` - (Optional) PEM-encoded root certificates bundle for TLS authentication. Can be sourced from `KUBE_CLUSTER_CA_CERT_DATA`.
Expand Down

0 comments on commit 39a6b40

Please sign in to comment.