|
| 1 | +package extended |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + b64 "encoding/base64" |
| 8 | + |
| 9 | + exutil "github.com/openshift/machine-config-operator/test/extended-priv/util" |
| 10 | + logger "github.com/openshift/machine-config-operator/test/extended-priv/util/logext" |
| 11 | + "github.com/tidwall/gjson" |
| 12 | +) |
| 13 | + |
| 14 | +// ControllerConfig struct is used to handle ControllerConfig resources in OCP |
| 15 | +type ControllerConfig struct { |
| 16 | + Resource |
| 17 | +} |
| 18 | + |
| 19 | +// CertificateInfo stores the information regarding a given certificate |
| 20 | +type CertificateInfo struct { |
| 21 | + // subject is the cert subject |
| 22 | + Subject string `json:"subject"` |
| 23 | + |
| 24 | + // signer is the cert Issuer |
| 25 | + Signer string `json:"signer"` |
| 26 | + |
| 27 | + // notBefore is the lower boundary for validity |
| 28 | + NotBefore string `json:"notBefore"` |
| 29 | + |
| 30 | + // notAfter is the upper boundary for validity |
| 31 | + NotAfter string `json:"notAfter"` |
| 32 | + |
| 33 | + // bundleFile is the larger bundle a cert comes from |
| 34 | + BundleFile string `json:"bundleFile"` |
| 35 | +} |
| 36 | + |
| 37 | +// NewControllerConfig create a ControllerConfig struct |
| 38 | +func NewControllerConfig(oc *exutil.CLI, name string) *ControllerConfig { |
| 39 | + return &ControllerConfig{Resource: *NewResource(oc, "ControllerConfig", name)} |
| 40 | +} |
| 41 | + |
| 42 | +// GetKubeAPIServerServingCAData return the base64 decoded value of the kubeAPIServerServingCAData bundle stored in the ControllerConfig |
| 43 | +func (cc *ControllerConfig) GetKubeAPIServerServingCAData() (string, error) { |
| 44 | + b64KubeAPIServerServingData, err := cc.Get(`{.spec.kubeAPIServerServingCAData}`) |
| 45 | + if err != nil { |
| 46 | + return "", err |
| 47 | + } |
| 48 | + |
| 49 | + kubeAPIServerServingCAData, err := b64.StdEncoding.DecodeString(b64KubeAPIServerServingData) |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + return string(kubeAPIServerServingCAData), err |
| 54 | +} |
| 55 | + |
| 56 | +// GetRootCAData return the base64 decoded value of the rootCA bundle stored in the ControllerConfig |
| 57 | +func (cc *ControllerConfig) GetRootCAData() (string, error) { |
| 58 | + b64RootCAData, err := cc.Get(`{.spec.rootCAData}`) |
| 59 | + if err != nil { |
| 60 | + return "", err |
| 61 | + } |
| 62 | + |
| 63 | + rootCAData, err := b64.StdEncoding.DecodeString(b64RootCAData) |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + return string(rootCAData), err |
| 68 | +} |
| 69 | + |
| 70 | +// GetImageRegistryBundleData returns a map[string]string containing the filenames and values of the image registry bundle data |
| 71 | +func (cc *ControllerConfig) GetImageRegistryBundleData() (map[string]string, error) { |
| 72 | + return cc.GetImageRegistryBundle("imageRegistryBundleData") |
| 73 | +} |
| 74 | + |
| 75 | +// GetImageRegistryBundleUserData returns a map[string]string containing the filenames and values of the image registry bundle user data |
| 76 | +func (cc *ControllerConfig) GetImageRegistryBundleUserData() (map[string]string, error) { |
| 77 | + return cc.GetImageRegistryBundle("imageRegistryBundleUserData") |
| 78 | +} |
| 79 | + |
| 80 | +// GetImageRegistryBundle returns a map[string]string containing the filenames and values of the image registry certificates in a Bundle field |
| 81 | +func (cc *ControllerConfig) GetImageRegistryBundle(bundleField string) (map[string]string, error) { |
| 82 | + certs := map[string]string{} |
| 83 | + |
| 84 | + bundleData, err := cc.Get(`{.spec.` + bundleField + `}`) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + parsedBundleData := gjson.Parse(bundleData) |
| 90 | + |
| 91 | + var b64Err error |
| 92 | + parsedBundleData.ForEach(func(_, item gjson.Result) bool { |
| 93 | + file := item.Get("file").String() |
| 94 | + data64 := item.Get("data").String() |
| 95 | + |
| 96 | + data, b64Err := b64.StdEncoding.DecodeString(data64) |
| 97 | + if err != nil { |
| 98 | + logger.Infof("Error decoding data for image registry bundle file %s: %s", file, b64Err) |
| 99 | + return false // stop iterating |
| 100 | + } |
| 101 | + |
| 102 | + certs[file] = string(data) |
| 103 | + return true // keep iterating |
| 104 | + }) |
| 105 | + if b64Err != nil { |
| 106 | + return nil, b64Err |
| 107 | + } |
| 108 | + |
| 109 | + return certs, nil |
| 110 | +} |
| 111 | + |
| 112 | +// GetImageRegistryBundleByFileName returns the image registry bundle searching by bundle filename |
| 113 | +func (cc *ControllerConfig) GetImageRegistryBundleDataByFileName(fileName string) (string, error) { |
| 114 | + certs, err := cc.GetImageRegistryBundleData() |
| 115 | + if err != nil { |
| 116 | + return "", err |
| 117 | + } |
| 118 | + |
| 119 | + data, ok := certs[fileName] |
| 120 | + if !ok { |
| 121 | + return "", fmt.Errorf("There is no image registry bundle with file name %s", fileName) |
| 122 | + } |
| 123 | + |
| 124 | + return data, nil |
| 125 | +} |
| 126 | + |
| 127 | +// GetImageRegistryUserBundleByFileName returns the image registry bundle searching by bundle filename |
| 128 | +func (cc *ControllerConfig) GetImageRegistryBundleUserDataByFileName(fileName string) (string, error) { |
| 129 | + certs, err := cc.GetImageRegistryBundleUserData() |
| 130 | + if err != nil { |
| 131 | + return "", err |
| 132 | + } |
| 133 | + |
| 134 | + data, ok := certs[fileName] |
| 135 | + if !ok { |
| 136 | + return "", fmt.Errorf("There is no image registry bundle with file name %s", fileName) |
| 137 | + } |
| 138 | + |
| 139 | + return data, nil |
| 140 | +} |
| 141 | + |
| 142 | +// Returns a list of CertificateInfo structs with the information of all the certificates tracked by ControllerConfig |
| 143 | +func (cc *ControllerConfig) GetCertificatesInfo() ([]CertificateInfo, error) { |
| 144 | + certsInfoString := cc.GetOrFail(`{.status.controllerCertificates}`) |
| 145 | + |
| 146 | + logger.Debugf("CERTIFICATES: %s", certsInfoString) |
| 147 | + |
| 148 | + var certsInfo []CertificateInfo |
| 149 | + |
| 150 | + jsonerr := json.Unmarshal([]byte(certsInfoString), &certsInfo) |
| 151 | + |
| 152 | + if jsonerr != nil { |
| 153 | + return nil, jsonerr |
| 154 | + } |
| 155 | + |
| 156 | + return certsInfo, nil |
| 157 | +} |
| 158 | + |
| 159 | +func (cc *ControllerConfig) GetCertificatesInfoByBundleFileName(bundleFile string) ([]CertificateInfo, error) { |
| 160 | + |
| 161 | + var certsInfo []CertificateInfo |
| 162 | + |
| 163 | + allCertsInfo, err := cc.GetCertificatesInfo() |
| 164 | + if err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + |
| 168 | + for _, ciLoop := range allCertsInfo { |
| 169 | + ci := ciLoop |
| 170 | + if ci.BundleFile == bundleFile { |
| 171 | + certsInfo = append(certsInfo, ci) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return certsInfo, nil |
| 176 | +} |
0 commit comments