Skip to content

Commit 7aa9bc9

Browse files
committed
openstack-manila: Consume CA cert from credentials secret (controller)
Do what we previously did for the openstack-cinder controller but for the openstack-manila controller. In effect, we're really just reflecting the changes made in cluster-storage-operator in [1]. However, we do need to add some logic to detect where we are consuming our CA cert from so that we can match forthcoming changes to our assets. While here, we also replace use of the deprecated `ioutil.ReadFile` function in favour of its suggested replacement, `os.ReadFile` [2]. We also replace use of `os.IsNotExist` in favour of its suggested replacement, `errors.Is(err, fs.ErrNotExist)` [3]. [1] github.com/openshift/cluster-storage-operator/pull/557 [2] https://pkg.go.dev/io/ioutil#ReadFile [3] https://pkg.go.dev/os#IsNotExist Signed-off-by: Stephen Finucane <[email protected]>
1 parent 4509b9e commit 7aa9bc9

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

pkg/openstack-manila/client/openstack.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"fmt"
8-
"io/ioutil"
99
"net/http"
1010
"os"
1111
"time"
@@ -59,7 +59,7 @@ func (o *openStackClient) GetShareTypes() ([]sharetypes.ShareType, error) {
5959
provider.UserAgent = ua
6060

6161
cert, err := getCloudProviderCert()
62-
if err != nil && !os.IsNotExist(err) {
62+
if err != nil && !errors.Is(err, os.ErrNotExist) {
6363
return nil, fmt.Errorf("failed to get cloud provider CA certificate: %w", err)
6464
}
6565

@@ -103,7 +103,7 @@ func (o *openStackClient) GetShareTypes() ([]sharetypes.ShareType, error) {
103103
}
104104

105105
func getCloudFromFile(filename string) (*clientconfig.Cloud, error) {
106-
cloudConfig, err := ioutil.ReadFile(filename)
106+
cloudConfig, err := os.ReadFile(filename)
107107
if err != nil {
108108
return nil, err
109109
}
@@ -121,5 +121,11 @@ func getCloudFromFile(filename string) (*clientconfig.Cloud, error) {
121121
}
122122

123123
func getCloudProviderCert() ([]byte, error) {
124-
return ioutil.ReadFile(util.CertFile)
124+
data, err := os.ReadFile(util.CertFile)
125+
if err == nil || !errors.Is(err, os.ErrNotExist) {
126+
return data, err
127+
}
128+
129+
// legacy path; remove in 4.20
130+
return os.ReadFile(util.LegacyCertFile)
125131
}

pkg/openstack-manila/secret/secretsync.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type SecretSyncController struct {
3535
const (
3636
// Name of key with clouds.yaml in Secret provided by cloud-credentials-operator.
3737
cloudSecretKey = "clouds.yaml"
38-
// Name of OpenStack in clouds.yaml
39-
// Canonical path for custom ca certificates
40-
cacertPath = "/etc/kubernetes/static-pod-resources/configmaps/cloud-config/ca-bundle.pem"
38+
// Path for custom CA certificates when provided by cloud-credentials-operator.
39+
defaultCACertPath = "/etc/openstack/ca.crt"
40+
// Path for custom CA certificates when provided by Installer (legacy path).
41+
legacyCACertPath = "/etc/kubernetes/static-pod-resources/configmaps/cloud-config/ca-bundle.pem"
4142
)
4243

4344
func NewSecretSyncController(
@@ -115,11 +116,29 @@ func (c *SecretSyncController) translateSecret(cloudSecret *v1.Secret) (*v1.Secr
115116

116117
data := cloudToConf(cloud)
117118

118-
// In the hypershift secret, the clouds.yaml field might not have the cacert defined. The content of the certificate
119-
// is defined in the ca-bundle.pem field instead.
120-
_, ok = cloudSecret.Data["ca-bundle.pem"]
121-
if ok {
122-
data["os-certAuthorityPath"] = []byte(cacertPath)
119+
// Determine where our CA cert is stored.
120+
// TODO(stephenfin): Remove most of this in 4.20+
121+
var caCertPath string
122+
if _, ok := cloudSecret.Data["cacert"]; ok {
123+
// Option A: We have the CA cert in our credentials under the 'cacert'
124+
// key which indicates a recent (>= 4.19) version of
125+
// cluster-credential-operator (CCO) or hypershift
126+
caCertPath = defaultCACertPath
127+
} else if _, ok = cloudSecret.Data["ca-bundle.pem"]; ok {
128+
// Option B: We have the CA cert in our credentials but under the
129+
// 'ca-bundle.pem' key, which indicates an older (< 4.19) version of
130+
// hypershift
131+
caCertPath = legacyCACertPath
132+
} else if cloud.CACertFile != "" {
133+
// Option C: We have a non-empty 'cafile' field in our clouds.yaml.
134+
// This means our root credential secret has this defined yet
135+
// cloud-credential-operator (CCO) didn't populate the 'cacert' key of
136+
// the secret. This indicates an older (< 4.19) version of CCO.
137+
caCertPath = legacyCACertPath
138+
}
139+
140+
if caCertPath != "" {
141+
data["os-certAuthorityPath"] = []byte(caCertPath)
123142
}
124143

125144
secret := v1.Secret{
@@ -182,10 +201,8 @@ func cloudToConf(cloud clientconfig.Cloud) map[string][]byte {
182201
data["os-userDomainName"] = []byte(cloud.AuthInfo.UserDomainName)
183202
data["os-domainName"] = []byte(cloud.AuthInfo.UserDomainName)
184203
}
185-
if cloud.CACertFile != "" {
186-
// Replace the original cert authority path from clouds.yaml with the canonical one
187-
data["os-certAuthorityPath"] = []byte(cacertPath)
188-
}
204+
205+
// We don't set os-certAuthorityPath here as it's handled separately.
189206

190207
return data
191208
}

pkg/openstack-manila/util/const.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const (
1111

1212
// OpenStack config file name (as present in the operator Deployment)
1313
CloudConfigFilename = "/etc/openstack/clouds.yaml"
14-
CertFile = "/etc/openstack-ca/ca-bundle.pem"
14+
CertFile = "/etc/openstack/ca.crt"
15+
LegacyCertFile = "/etc/openstack-ca/ca-bundle.pem"
1516

1617
// Name of cloud in secret provided by cloud-credentials-operator
1718
CloudName = "openstack"

0 commit comments

Comments
 (0)