Skip to content
This repository has been archived by the owner on Jun 24, 2020. It is now read-only.

Remove the newly generated key if sc add-gcp-broker command fails #133

Merged
merged 1 commit into from
Feb 12, 2018

Conversation

maqiuyujoyce
Copy link
Contributor

Fixes #38

return fmt.Errorf("error generating configs for GCP :: %v", err)
}

err = deployGCPBrokerConfigs(dir)
if err != nil {
// Clean up the newly generated key if the command failed.
cleanupNewKey(brokerSAEmail, key)
return fmt.Errorf("error deploying GCP broker configs :%v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Group as a helper function, and then do something like:

if err := foo(a, b, c, ..); err != nil {
  cleanupNewKey(brokerSAEmail, key)
  return ...
}

Copy link
Contributor Author

@maqiuyujoyce maqiuyujoyce Feb 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to group getOrCreateVirtualBroker(), generateGCPBrokerConfigs(), and deployGCPBrokerConfigs() together?

In that case, I'd prefer to return the key to the NewXXXCommand method and let it handle the cleanup when the key is not empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling cleanupNewKey in multiple places makes further change prone to introduce a bug. Some simple option of avoiding it is creating one helper function where you put all the cases that should call cleanupNewKey(). If you have other way to achieve it, please feel free to try that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will keep as is and clean up/refactor the entire tool when we have time. Tracked in #149.

@@ -280,6 +287,25 @@ func httpAdapterFromAuthKey(keyFile string) (*adapter.HttpAdapter, error) {
return adapter.NewHttpAdapter(client), nil
}

// cleanupNewKey removes the newly generated service account key.
func cleanupNewKey(email, key string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the newly generated/a given/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is more than cleanup the "given key". It is to cleanup the given keyfile content in base64 encoding. I'll update the comment to be more specific.

return
}

keyID := keyJson["private_key_id"].(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a chance of panic due to ".(string)" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked in #149.

out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("failed to delete service account key: %s : %v\n", string(out), err)
fmt.Printf("WARNING: Please clean up the key from service account %s", email)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Error: failed to delete ..."
"Warning: please ..." ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current error message is actually "freeform" and based on the experience of each developer. We need to do a bit design and clean it up. Tracked by #150.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants