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 support for OpenStack application credentials and update validation #1666

Merged
merged 5 commits into from
Dec 7, 2021
Merged
Changes from 1 commit
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
88 changes: 62 additions & 26 deletions pkg/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,29 @@ import (
// The environment variable names with credential in them
const (
// Variables that KubeOne (and Terraform) expect to see
AWSAccessKeyID = "AWS_ACCESS_KEY_ID"
AWSSecretAccessKey = "AWS_SECRET_ACCESS_KEY" //nolint:gosec
AzureClientID = "ARM_CLIENT_ID"
AzureClientSecret = "ARM_CLIENT_SECRET" //nolint:gosec
AzureTenantID = "ARM_TENANT_ID"
AzureSubscribtionID = "ARM_SUBSCRIPTION_ID"
DigitalOceanTokenKey = "DIGITALOCEAN_TOKEN"
GoogleServiceAccountKey = "GOOGLE_CREDENTIALS"
HetznerTokenKey = "HCLOUD_TOKEN"
OpenStackAuthURL = "OS_AUTH_URL"
OpenStackDomainName = "OS_DOMAIN_NAME"
OpenStackPassword = "OS_PASSWORD"
OpenStackRegionName = "OS_REGION_NAME"
OpenStackTenantID = "OS_TENANT_ID"
OpenStackTenantName = "OS_TENANT_NAME"
OpenStackUserName = "OS_USERNAME"
PacketAPIKey = "PACKET_AUTH_TOKEN" //nolint:gosec
PacketProjectID = "PACKET_PROJECT_ID"
VSphereAddress = "VSPHERE_SERVER"
VSpherePassword = "VSPHERE_PASSWORD"
VSphereUsername = "VSPHERE_USER"
AWSAccessKeyID = "AWS_ACCESS_KEY_ID"
AWSSecretAccessKey = "AWS_SECRET_ACCESS_KEY" //nolint:gosec
AzureClientID = "ARM_CLIENT_ID"
AzureClientSecret = "ARM_CLIENT_SECRET" //nolint:gosec
AzureTenantID = "ARM_TENANT_ID"
AzureSubscribtionID = "ARM_SUBSCRIPTION_ID"
DigitalOceanTokenKey = "DIGITALOCEAN_TOKEN"
GoogleServiceAccountKey = "GOOGLE_CREDENTIALS"
HetznerTokenKey = "HCLOUD_TOKEN"
OpenStackAuthURL = "OS_AUTH_URL"
OpenStackDomainName = "OS_DOMAIN_NAME"
OpenStackPassword = "OS_PASSWORD"
OpenStackRegionName = "OS_REGION_NAME"
OpenStackTenantID = "OS_TENANT_ID"
OpenStackTenantName = "OS_TENANT_NAME"
OpenStackUserName = "OS_USERNAME"
OpenStackApplicationCredentialID = "OS_APPLICATION_CREDENTIAL_ID"
OpenStackApplicationCredentialSecret = "OS_APPLICATION_CREDENTIAL_SECRET"
PacketAPIKey = "PACKET_AUTH_TOKEN" //nolint:gosec
PacketProjectID = "PACKET_PROJECT_ID"
VSphereAddress = "VSPHERE_SERVER"
VSpherePassword = "VSPHERE_PASSWORD"
VSphereUsername = "VSPHERE_USER"

// Variables that machine-controller expects
AzureClientIDMC = "AZURE_CLIENT_ID"
Expand Down Expand Up @@ -158,6 +160,8 @@ func ProviderCredentials(cloudProvider kubeone.CloudProviderSpec, credentialsFil
{Name: OpenStackAuthURL},
{Name: OpenStackUserName, MachineControllerName: OpenStackUserNameMC},
{Name: OpenStackPassword},
{Name: OpenStackApplicationCredentialID, MachineControllerName: OpenStackUserNameMC},
embik marked this conversation as resolved.
Show resolved Hide resolved
{Name: OpenStackApplicationCredentialSecret},
{Name: OpenStackDomainName},
{Name: OpenStackRegionName},
{Name: OpenStackTenantID},
Expand Down Expand Up @@ -283,13 +287,45 @@ func defaultValidationFunc(creds map[string]string) error {
}

func openstackValidationFunc(creds map[string]string) error {
for k, v := range creds {
if k == OpenStackTenantID || k == OpenStackTenantName {
continue
alwaysRequired := []string{OpenStackAuthURL, OpenStackDomainName, OpenStackRegionName}
xmudrii marked this conversation as resolved.
Show resolved Hide resolved

for _, key := range alwaysRequired {
if v, ok := creds[key]; !ok || len(v) == 0 {
return errors.Errorf("key %v is required but is not present", key)
}
if len(v) == 0 {
return errors.Errorf("key %v is required but isn't present", k)
}

var (
appCredsInUse bool
userCredsInUse bool
)

if v, ok := creds[OpenStackApplicationCredentialID]; ok && len(v) != 0 {
if v, ok := creds[OpenStackApplicationCredentialSecret]; !ok || len(v) == 0 {
return errors.Errorf("key %v is required if %v is present", OpenStackApplicationCredentialSecret, OpenStackApplicationCredentialID)
}
appCredsInUse = true
}
xmudrii marked this conversation as resolved.
Show resolved Hide resolved

if v, ok := creds[OpenStackUserName]; !appCredsInUse && ok && len(v) != 0 {
if v, ok := creds[OpenStackPassword]; !ok || len(v) == 0 {
return errors.Errorf("key %v is required but is not present", OpenStackPassword)
}
userCredsInUse = true
}
xmudrii marked this conversation as resolved.
Show resolved Hide resolved

if !appCredsInUse && !userCredsInUse {
return errors.Errorf("no app credentials (%s, %s) or user credentials (%s, %s) found",
OpenStackApplicationCredentialID, OpenStackApplicationCredentialSecret,
OpenStackUserName, OpenStackPassword,
)
}

if appCredsInUse && userCredsInUse {
return errors.Errorf("both app credentials (%s, %s) and user credentials (%s, %s) defined",
OpenStackApplicationCredentialID, OpenStackApplicationCredentialSecret,
OpenStackUserName, OpenStackPassword,
)
}
xmudrii marked this conversation as resolved.
Show resolved Hide resolved

if v, ok := creds[OpenStackTenantID]; !ok || len(v) == 0 {
Expand Down