Skip to content

fix: Replace \n strings with new lines in pem_file - #931

Merged
jcudit merged 4 commits into
integrations:mainfrom
shrink:fix-pem-file-environment-variable-new-lines
Nov 8, 2021
Merged

jcudit merged 4 commits into
integrations:mainfrom
shrink:fix-pem-file-environment-variable-new-lines

Conversation

@shrink

@shrink shrink commented Oct 7, 2021 •

Copy link
Copy Markdown
Contributor

#804 added environment variable input for pem_file but golang's pem decode functionality only works when the string contains actual new lines. Terraform Cloud does not permit new lines in Environment Variables, therefore it is currently not possible to use the GITHUB_APP_PEM_FILE Environment Variable on Terraform Cloud.

Unfortunately, there's no test coverage for the functionality in question so while this is, in principle, a very simple change, I'm not entirely sure if there are side effects I have not considered.

https://play.golang.org/p/h3Pn97nqF8j

@shrink

shrink commented Oct 7, 2021

Copy link
Copy Markdown
Contributor Author

A very helpful confirmation from @jspiro on the #804 has confirmed that the reference to app_pem working on Terraform Cloud was using Terraform Variables not Environment Variables.

We use a variable, just as you describe, in Terraform Cloud.

I am now confident that new-line decoding is the issue and that this is the appropriate fix.

@grahame-murray-db

Copy link
Copy Markdown

@jcudit Could this make it into 4.18?

@kfcampbell kfcampbell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had a couple comments...do you mind helping me understand these changes a little better?

Comment thread github/provider.go

if v, ok := appAuthAttr["pem_file"].(string); ok && v != "" {
appPemFile = v
appPemFile = strings.Replace(v, `\n`, "\n", -1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think it's worth writing a comment here to explain this behavior? I think it's not super obvious and probably worth documenting in some manner.

@shrink shrink Oct 31, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have added a comment to the code with some context.

I chose to add new line replacement in the provider's configuration code because it felt most appropriate as it is dealing with how the provider is configured, however, it could be more clear if the behaviour was added to the part of the provider where the PEM is consumed, i.e: https://github.com/integrations/terraform-provider-github/blob/main/github/apps.go#L19

func GenerateOAuthTokenFromApp(baseURL, appID, appInstallationID, pemData string) (string, error) {
+       pemData = strings.Replace(pemData, `\n`, "\n", -1)
	appJWT, err := generateAppJWT(appID, time.Now(), []byte(pemData))

Personally I think it belongs in the configuration code but I am happy to move it into the consuming code (either apps.go@GenerateOAuthTokenFromApp or apps.go@generateAppJWT) if you think that will be clearer about the behaviour and intent -- let me know if you do and I'll make the change!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for doing that! I agree with you that this change belongs at the configuration level, so all consumers can trust they have the correctly-escaped version of the certificate.

I was a little concerned that this might break existing certiricate handling somehow, but I believe that the base64-encoded nature of the certificates guarantees that there won't be \n string literals in the cert that we accidentally over-replace, so I think this is pretty safe.

Comment thread github/provider.go Resolved

@kfcampbell kfcampbell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My approval doesn't grant anything to this PR, but FWIW @jcudit I think this can be merged.

Comment thread github/provider.go

if v, ok := appAuthAttr["pem_file"].(string); ok && v != "" {
appPemFile = v
appPemFile = strings.Replace(v, `\n`, "\n", -1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for doing that! I agree with you that this change belongs at the configuration level, so all consumers can trust they have the correctly-escaped version of the certificate.

I was a little concerned that this might break existing certiricate handling somehow, but I believe that the base64-encoded nature of the certificates guarantees that there won't be \n string literals in the cert that we accidentally over-replace, so I think this is pretty safe.

@jcudit
jcudit merged commit 1d40ad2 into integrations:main Nov 8, 2021
kfcampbell pushed a commit to kfcampbell/terraform-provider-github that referenced this pull request Jul 26, 2022
* fix: Replace `\n` strings with new lines in pem_file

* chore: Format imports using alphabetical ordering

* docs: Describe replacement of `\n` with new lines in PEM file

* style: Remove erroneous trailing whitespace
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.

4 participants