Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@soeirosantos
Copy link
Contributor

We'd like to have Drone secrets in base64 to be passed directly to the GAE_CREDENTIALS param.

The main use case is to integrate directly with the Vault Google Cloud Secrets Engine that returns the service account key in base64.

Example:

---
kind: secret
name: google_credentials_b64
get:
  path: gcp/key/drone-gae-deployer
  name: private_key_data

---
kind: pipeline
name: default
steps:
[...]
- name: deploy
  pull: if-not-exists
  image: nytimes/drone-gae
  settings:
    action: deploy
    app_file: app.yaml
    max_versions: 2
    project: some-project
    version: ${DRONE_COMMIT}
    gae_credentials:
      from_secret: google_credentials_b64

@soeirosantos soeirosantos requested a review from a team as a code owner April 2, 2021 22:57
vargs.Token = os.Getenv("GAE_CREDENTIALS")
}

if decodedToken, err := base64.StdEncoding.DecodeString(vargs.Token); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this function able to figure out if the string that it's reading actually is encoded?

Copy link
Contributor Author

@soeirosantos soeirosantos Apr 5, 2021

Choose a reason for hiding this comment

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

Yes, the usual way to verify if a string is encoded in Go is to call DecodeString and check if err == nil.

Copy link
Contributor

@msuterski msuterski Apr 5, 2021

Choose a reason for hiding this comment

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

hmm, I'm not sure if it is 100% reliable

	encoded := "ZW5jb2RlZCBzZXJ2aWNlIGFjY291bnQga2V5"

	if decodedToken, err := base64.StdEncoding.DecodeString(encoded); err == nil {
		fmt.Println(string(decodedToken))
	}

	notencoded := "encodedserviceaccountkey"

	if decodedToken, err := base64.StdEncoding.DecodeString(notencoded); err == nil {
		fmt.Println(string(decodedToken))
	}

Both of these print for me (no error) and the second one is not base64 encoded string.

The error is returned when there are spaces or other non alphanumerics in the string.

The second one (decoding of not-encoded string) is producing garbage string but it seems it is a "valid" 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.

I got your point, that makes sense. I believe your test has this behavior because "encodedserviceaccountkey" is a "valid" base64 encoded string in terms of structure.

The main point is when an actual service account key json is provided the DecodeString function will fail and an error is going to be returned.

Another option (that I'd like to avoid if possible) would be to add a different field for the base64, like GAE_CREDENTIALS_B64. I'd like to avoid this approach because it unnecessarily changes the plugin interface. But I'm not totally against it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right that this should work in this type of field case.

I wonder if it would be helpful to indicate somehow to the users (log to the console) that decoding failed and it's going to be using the value as is. I'm thinking about cases where someone puts in invalid b64 string e.g. with extra whitespace at the end or similar (which was happening in the past).
The b64 values will come from an external source, not users in this use case, but who knows how people will use it in the future.

It may also be an overkill and what we have in the PR is good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I'll add the log output. It will definitely make it easier to debug when someone puts in an invalid base64 string.

Copy link
Member

Choose a reason for hiding this comment

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

Just a thought: how about if you check the credential can be unmarshalled into it's JSON structure first? If it doesn't, then try decoding it and unmarshalling?

That way you are doing validation for both structure types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @tonglil, I'm so sorry I haven't seen your comment before merging - just seeing it now. I think this is a good validation to have. In any case, the best validation is going to be provided by the gcloud command when it tries to activate the service account. I'm happy to add the JSON structure validation if that makes sense, tho.

Copy link
Member

Choose a reason for hiding this comment

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

Nah you're right, let's let gcloud do the heavy lifting

@soeirosantos soeirosantos requested a review from msuterski April 5, 2021 14:42
Copy link
Contributor

@msuterski msuterski left a comment

Choose a reason for hiding this comment

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

This is great! thanks Rom.

@soeirosantos soeirosantos merged commit ccfd513 into nytimes:master Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants