Skip to content

Commit

Permalink
Bug 1813949: ignore local env variables when we create a service client
Browse files Browse the repository at this point in the history
This commit explicitly disables reading auth data from env variables
by setting an invalid EnvPrefix. By doing this, we make sure that the
data from clouds.yaml is enough to authenticate.

By doing this we don't have to unset OS_CLOUD env variable explicitly
anymore.

Ref https://issues.redhat.com/browse/OSASINFRA-2152
  • Loading branch information
Fedosin committed Dec 2, 2020
1 parent 64ec239 commit 6e26d44
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
6 changes: 0 additions & 6 deletions pkg/asset/installconfig/openstack/openstack.go
Expand Up @@ -2,7 +2,6 @@
package openstack

import (
"os"
"sort"
"strings"

Expand Down Expand Up @@ -46,11 +45,6 @@ func Platform() (*openstack.Platform, error) {
return nil, errors.Wrap(err, "failed UserInput")
}

// We should unset OS_CLOUD env variable here, because the real cloud name was defined
// on the previous step. OS_CLOUD has more priority, so the value from "cloud" variable
// will be ignored if OS_CLOUD contains something.
os.Unsetenv("OS_CLOUD")

networkNames, err := getExternalNetworkNames(cloud)
if err != nil {
return nil, err
Expand Down
10 changes: 4 additions & 6 deletions pkg/asset/installconfig/openstack/session.go
Expand Up @@ -2,7 +2,6 @@
package openstack

import (
"os"
"sync"

"github.com/pkg/errors"
Expand All @@ -23,11 +22,6 @@ type Session struct {
func GetSession(cloudName string) (*Session, error) {
opts := defaultClientOpts(cloudName)

// We should unset OS_CLOUD env variable here, because the real cloud name was
// defined on the previous step. OS_CLOUD has more priority, so the value from
// "opts" variable will be ignored if OS_CLOUD contains something.
os.Unsetenv("OS_CLOUD")

cloudConfig, err := clientconfig.GetCloudFromYAML(opts)
if err != nil {
return nil, err
Expand All @@ -41,6 +35,10 @@ func defaultClientOpts(cloudName string) *clientconfig.ClientOpts {
opts := new(clientconfig.ClientOpts)
opts.Cloud = cloudName
opts.YAMLOpts = new(yamlLoadOpts)
// We explicitly disable reading auth data from env variables by setting an invalid EnvPrefix.
// By doing this, we make sure that the data from clouds.yaml is enough to authenticate.
// For more information: https://github.com/gophercloud/utils/blob/8677e053dcf1f05d0fa0a616094aace04690eb94/openstack/clientconfig/requests.go#L508
opts.EnvPrefix = "NO_ENV_VARIABLES_"
return opts
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/asset/installconfig/openstack/validation/cloudinfo.go
Expand Up @@ -2,7 +2,6 @@ package validation

import (
"net/url"
"os"
"strings"

"github.com/gophercloud/gophercloud"
Expand Down Expand Up @@ -57,10 +56,10 @@ func GetCloudInfo(ic *types.InstallConfig) (*CloudInfo, error) {

opts := &clientconfig.ClientOpts{Cloud: ic.OpenStack.Cloud}

// We should unset OS_CLOUD env variable here, because the real cloud name was
// defined on the previous step. OS_CLOUD has more priority, so the value from
// "opts" variable will be ignored if OS_CLOUD contains something.
os.Unsetenv("OS_CLOUD")
// We explicitly disable reading auth data from env variables by setting an invalid EnvPrefix.
// By doing this, we make sure that the data from clouds.yaml is enough to authenticate.
// For more information: https://github.com/gophercloud/utils/blob/8677e053dcf1f05d0fa0a616094aace04690eb94/openstack/clientconfig/requests.go#L508
opts.EnvPrefix = "NO_ENV_VARIABLES_"

ci.clients.networkClient, err = clientconfig.NewServiceClient("network", opts)
if err != nil {
Expand Down
22 changes: 13 additions & 9 deletions pkg/asset/installconfig/openstack/validvaluesfetcher.go
Expand Up @@ -28,9 +28,7 @@ func getCloudNames() ([]string, error) {
// getExternalNetworkNames interrogates OpenStack to get the external network
// names.
func getExternalNetworkNames(cloud string) ([]string, error) {
conn, err := clientconfig.NewServiceClient("network", &clientconfig.ClientOpts{
Cloud: cloud,
})
conn, err := clientconfig.NewServiceClient("network", defaultValidValuesFetcherClientOpts(cloud))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -61,9 +59,7 @@ func getExternalNetworkNames(cloud string) ([]string, error) {

// getFlavorNames gets a list of valid flavor names.
func getFlavorNames(cloud string) ([]string, error) {
conn, err := clientconfig.NewServiceClient("compute", &clientconfig.ClientOpts{
Cloud: cloud,
})
conn, err := clientconfig.NewServiceClient("compute", defaultValidValuesFetcherClientOpts(cloud))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,9 +87,7 @@ func getFlavorNames(cloud string) ([]string, error) {
return flavorNames, nil
}
func getFloatingIPNames(cloud string, floatingNetworkName string) ([]string, error) {
conn, err := clientconfig.NewServiceClient("network", &clientconfig.ClientOpts{
Cloud: cloud,
})
conn, err := clientconfig.NewServiceClient("network", defaultValidValuesFetcherClientOpts(cloud))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -131,3 +125,13 @@ func getFloatingIPNames(cloud string, floatingNetworkName string) ([]string, err

return floatingIPNames, nil
}

func defaultValidValuesFetcherClientOpts(cloudName string) *clientconfig.ClientOpts {
opts := new(clientconfig.ClientOpts)
opts.Cloud = cloudName
// We explicitly disable reading auth data from env variables by setting an invalid EnvPrefix.
// By doing this, we make sure that the data from clouds.yaml is enough to authenticate.
// For more information: https://github.com/gophercloud/utils/blob/8677e053dcf1f05d0fa0a616094aace04690eb94/openstack/clientconfig/requests.go#L508
opts.EnvPrefix = "NO_ENV_VARIABLES_"
return opts
}
9 changes: 4 additions & 5 deletions pkg/destroy/openstack/openstack.go
Expand Up @@ -103,13 +103,12 @@ func (o *ClusterUninstaller) Run() error {

opts := &clientconfig.ClientOpts{
Cloud: o.Cloud,
// We explicitly disable reading auth data from env variables by setting an invalid EnvPrefix.
// By doing this, we make sure that the data from clouds.yaml is enough to authenticate.
// For more information: https://github.com/gophercloud/utils/blob/8677e053dcf1f05d0fa0a616094aace04690eb94/openstack/clientconfig/requests.go#L508
EnvPrefix: "NO_ENV_VARIABLES_",
}

// We should unset OS_CLOUD env variable here, because the real cloud name was
// defined on the previous step. OS_CLOUD has more priority, so the value from
// "opts" variable will be ignored if OS_CLOUD contains something.
os.Unsetenv("OS_CLOUD")

// launch goroutines
for name, function := range deleteFuncs {
go deleteRunner(name, function, opts, o.Filter, o.Logger, returnChannel)
Expand Down

0 comments on commit 6e26d44

Please sign in to comment.