Skip to content

Commit 7023e84

Browse files
Merge pull request #742 from hasueki/bug-2015800
Bug 2015800: fix(ibmcos): Resource key validation + update status granularly
2 parents 37a7cc1 + 8d243c2 commit 7023e84

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

pkg/storage/ibmcos/ibmcos.go

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
135135
d.Config.Location = infra.Status.PlatformStatus.IBMCloud.Location
136136
d.Config.ResourceGroupName = infra.Status.PlatformStatus.IBMCloud.ResourceGroupName
137137

138+
// Initialize IBMCOS status
139+
if cr.Status.Storage.IBMCOS == nil {
140+
cr.Status.Storage.IBMCOS = &imageregistryv1.ImageRegistryConfigStorageIBMCOS{}
141+
}
142+
138143
// Get resource controller service
139144
rc, err := d.getResouceControllerService()
140145
if err != nil {
@@ -176,9 +181,8 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
176181
// Set resource group name
177182
d.Config.ResourceGroupName = *rg.Name
178183
}
179-
cr.Status.Storage = imageregistryv1.ImageRegistryConfigStorage{
180-
IBMCOS: d.Config.DeepCopy(),
181-
}
184+
cr.Status.Storage.IBMCOS.ServiceInstanceCRN = d.Config.ServiceInstanceCRN
185+
cr.Status.Storage.IBMCOS.ResourceGroupName = d.Config.ResourceGroupName
182186
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
183187
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionFalse, "IBM COS Instance Active", "IBM COS service instance is active")
184188
case resourcecontrollerv2.ListResourceInstancesOptionsStateProvisioningConst:
@@ -261,15 +265,15 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
261265
}
262266

263267
d.Config.ServiceInstanceCRN = *instance.CRN
264-
cr.Status.Storage = imageregistryv1.ImageRegistryConfigStorage{
265-
IBMCOS: d.Config.DeepCopy(),
266-
}
268+
cr.Status.Storage.IBMCOS.ServiceInstanceCRN = d.Config.ServiceInstanceCRN
269+
cr.Status.Storage.IBMCOS.ResourceGroupName = d.Config.ResourceGroupName
270+
cr.Status.Storage.ManagementState = cr.Spec.Storage.ManagementState
267271
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
268272
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionFalse, "IBM COS Instance Creation Successful", "IBM COS service instance was successfully created")
269273
}
270274

271-
// Create resource key
272275
if len(d.Config.ResourceKeyCRN) == 0 {
276+
// Create resource key
273277
keyName := fmt.Sprintf("%s-%s", infra.Status.InfrastructureName, defaults.ImageRegistryName)
274278
roleCRN := "crn:v1:bluemix:public:iam::::serviceRole:Writer"
275279
params := &resourcecontrollerv2.ResourceKeyPostParameters{}
@@ -289,11 +293,43 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
289293
}
290294

291295
d.Config.ResourceKeyCRN = *key.CRN
292-
cr.Status.Storage = imageregistryv1.ImageRegistryConfigStorage{
293-
IBMCOS: d.Config.DeepCopy(),
294-
}
296+
cr.Status.Storage.IBMCOS.ResourceKeyCRN = d.Config.ResourceKeyCRN
295297
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
296298
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionFalse, "IBM COS Resource Key Creation Successful", "IBM COS resource key was successfully created")
299+
} else {
300+
// Get resource key
301+
key, resp, err := rc.GetResourceKeyWithContext(
302+
d.Context,
303+
&resourcecontrollerv2.GetResourceKeyOptions{
304+
ID: &d.Config.ResourceKeyCRN,
305+
},
306+
)
307+
if err != nil {
308+
return fmt.Errorf("unable to get resource key for service instance: %s with resp code: %d", err.Error(), resp.StatusCode)
309+
}
310+
311+
// Check if resource key is for service instance
312+
if *key.SourceCRN != d.Config.ServiceInstanceCRN {
313+
return fmt.Errorf("specified resource key is not valid for service instance")
314+
}
315+
316+
if key.Credentials != nil {
317+
// Check if resource key is HMAC enabled
318+
if key.Credentials.GetProperty("cos_hmac_keys") == nil {
319+
return fmt.Errorf("specified resource key credentials does not contain HMAC keys")
320+
}
321+
// Check if resource key has a valid IAM role
322+
if *key.Credentials.IamRoleCRN != "crn:v1:bluemix:public:iam::::serviceRole:Writer" && *key.Credentials.IamRoleCRN != "crn:v1:bluemix:public:iam::::serviceRole:Manager" {
323+
return fmt.Errorf("specified resource key's IAM role is not valid")
324+
}
325+
// Valid resource key
326+
d.Config.ResourceKeyCRN = *key.CRN
327+
cr.Status.Storage.IBMCOS.ResourceKeyCRN = d.Config.ResourceKeyCRN
328+
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
329+
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionFalse, "IBM COS Resource Key Valid", "IBM COS resource key exists and is valid")
330+
} else {
331+
return fmt.Errorf("specified resource key does not have any attached credentials")
332+
}
297333
}
298334

299335
// Check if bucket already exists
@@ -360,15 +396,6 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
360396
return err
361397
}
362398

363-
if cr.Spec.Storage.ManagementState == "" {
364-
cr.Spec.Storage.ManagementState = imageregistryv1.StorageManagementStateManaged
365-
}
366-
cr.Status.Storage = imageregistryv1.ImageRegistryConfigStorage{
367-
IBMCOS: d.Config.DeepCopy(),
368-
}
369-
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
370-
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionTrue, "Creation Successful", "IBM COS bucket was successfully created")
371-
372399
// Wait until the bucket exists
373400
if err := client.WaitUntilBucketExistsWithContext(
374401
d.Context,
@@ -381,6 +408,15 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error {
381408
}
382409
return err
383410
}
411+
412+
if cr.Spec.Storage.ManagementState == "" {
413+
cr.Spec.Storage.ManagementState = imageregistryv1.StorageManagementStateManaged
414+
}
415+
cr.Status.Storage = imageregistryv1.ImageRegistryConfigStorage{
416+
IBMCOS: d.Config.DeepCopy(),
417+
}
418+
cr.Spec.Storage.IBMCOS = d.Config.DeepCopy()
419+
util.UpdateCondition(cr, defaults.StorageExists, operatorapi.ConditionTrue, "Creation Successful", "IBM COS bucket was successfully created")
384420
}
385421

386422
return nil

0 commit comments

Comments
 (0)