Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tls_server_name provider options. #1638

Merged
merged 6 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
bartoszj marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -523,7 +529,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 @@ -539,14 +545,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