-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanitize exported stack state by scrubbing secrets #107
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ecff2e3
typo in README
thomas11 b608e15
Scrub secrets from exported stack state
thomas11 3fed63e
Remove duplicate code
thomas11 2c9f525
The plaintext secret must be a valid JSON string
thomas11 53cd67e
Add test that 'plaintext' is valid JSON after sanitization
thomas11 64bdfca
Add gRPC log sanitize method
danielrbradley 4c63294
Fix tests
danielrbradley dd28ae6
Allow partial and missing fields in gRPC logs
danielrbradley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sanitize | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype" | ||
) | ||
|
||
const plaintextSub = "REDACTED BY PROVIDERTEST" | ||
const secretSignature = "4dabf18193072939515e22adb298388d" | ||
|
||
// SanitizeSecretsInStackState sanitizes secrets in the stack state by replacing them with a placeholder. | ||
// secrets are identified by their magic signature, copied from pulumi/pulumi. | ||
func SanitizeSecretsInStackState(stack *apitype.UntypedDeployment) (*apitype.UntypedDeployment, error) { | ||
var d apitype.DeploymentV3 | ||
err := json.Unmarshal(stack.Deployment, &d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sanitizeSecretsInResources(d.Resources) | ||
|
||
marshaledDeployment, err := json.Marshal(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &apitype.UntypedDeployment{ | ||
Version: stack.Version, | ||
Deployment: json.RawMessage(marshaledDeployment), | ||
}, nil | ||
} | ||
|
||
func SanitizeSecretsInGrpcLog(log json.RawMessage) json.RawMessage { | ||
var data map[string]any | ||
if err := json.Unmarshal(log, &data); err != nil { | ||
return log | ||
} | ||
|
||
sanitized := sanitizeSecretsInObject(data, map[string]any{ | ||
secretSignature: "1b47061264138c4ac30d75fd1eb44270", | ||
"value": plaintextSub, | ||
}) | ||
sanitizedBytes, err := json.Marshal(sanitized) | ||
if err != nil { | ||
return log | ||
} | ||
return sanitizedBytes | ||
} | ||
|
||
func sanitizeSecretsInResources(resources []apitype.ResourceV3) { | ||
for i, r := range resources { | ||
r.Inputs = sanitizeSecretsInObject(r.Inputs, stateSecretReplacement) | ||
r.Outputs = sanitizeSecretsInObject(r.Outputs, stateSecretReplacement) | ||
resources[i] = r | ||
} | ||
} | ||
|
||
var stateSecretReplacement = map[string]any{ | ||
secretSignature: "1b47061264138c4ac30d75fd1eb44270", | ||
"plaintext": `"` + plaintextSub + `"`, // must be valid JSON, hence quoted | ||
} | ||
|
||
func sanitizeSecretsInObject(obj map[string]any, secretReplacement map[string]any) map[string]any { | ||
copy := map[string]any{} | ||
for k, v := range obj { | ||
innerObj, ok := v.(map[string]any) | ||
if ok { | ||
_, hasSecret := innerObj[secretSignature] | ||
if hasSecret { | ||
copy[k] = secretReplacement | ||
} else { | ||
copy[k] = sanitizeSecretsInObject(innerObj, secretReplacement) | ||
} | ||
} else { | ||
copy[k] = v | ||
} | ||
} | ||
return copy | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this break the tests upon reading the state back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps some tests are sensitive to the secret values and some aren't. Make it optional so we can opt in or out on a per-test basis. I'd prefer eventually pulumi/pulumi#17158 as I have a hunch that "official" encrypting should also be able to decrypt upon reading, given a fixed secrets provider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pulumi/pulumi#17158 would be great, but we should never persist and commit secrets. GH will reject them on push anyway. Not sure how a test could be not sensitive to them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With 17158 they're persisted in encrypted form, but you're right that it might be that the passphrase or encrypting key is plaintexted somewhere in our libs, we'd need to sanity check them. To elaborate the test being sensitive, I mean that you might get a test failing if you replace a value with
"REDACTED BY PROVIDERTEST"
sometimes, it would seem? If the test actually cares about the value? Maybe it's rare in practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The passphrase is definitely hard coded. I think, for the time being a simple hard-coded value is fine for most situations. If this causes an issue in the future then we could try do do something smarter where we mirror the shape of the secret data while entirely replacing it. E.g. if it's not a string but instead an object, then sanitise all the values within the object. In practice, in tests I think 99% of secrets will be individual strings, and might not even be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, IMO the cleanest is pulumi/pulumi#17158 + keeping your passphrase in GH secrets instead of sources gets you there as Pulumi already has the sanitization machinery for stack state. As to storing gRPC logs, encrypt-at-rest could do the job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But thanks for getting something working quickly!