Skip to content
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 8 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ snapshots need to be recorded anew on the new version.

### Fixing failing tests
- If the tests fail by flagging unwanted resource updates or replacements that are actually
acceptable, configure or custom
acceptable, configure a custom
[DiffValidation](https://github.com/pulumi/providertest/blob/5f23c3ec7cee882392ea356a54c0f74f56b0f7d5/upgrade.go#L241)
setting with more relaxed asserts.

Expand Down
9 changes: 8 additions & 1 deletion pulumitest/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/pulumi/providertest/pulumitest/optrun"
"github.com/pulumi/providertest/pulumitest/sanitize"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
)

Expand Down Expand Up @@ -42,8 +43,14 @@ func (pulumiTest *PulumiTest) Run(t PT, execute func(test *PulumiTest), opts ...
execute(isolatedTest)
exportedStack := isolatedTest.ExportStack(t)
if options.EnableCache {
ptLogF(t, "sanitizing secrets from stack state")
sanitizedStack, err := sanitize.SanitizeSecretsInStackState(&exportedStack)
if err != nil {
ptError(t, "failed to sanitize secrets from stack state: %v", err)
}

ptLogF(t, "writing stack state to %s", options.CachePath)
err = writeStackExport(options.CachePath, &exportedStack, false /* overwrite */)
err = writeStackExport(options.CachePath, sanitizedStack, false /* overwrite */)
if err != nil {
ptFatalF(t, "failed to write snapshot to %s: %v", options.CachePath, err)
}
Expand Down
77 changes: 77 additions & 0 deletions pulumitest/sanitize/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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"
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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!

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 sanitizeSecretsInResources(resources []apitype.ResourceV3) {
for i, r := range resources {
r.Inputs = sanitizeSecretsInObject(r.Inputs)
r.Outputs = sanitizeSecretsInObject(r.Outputs)
resources[i] = r
}
}

var secretReplacement = map[string]any{
secretSignature: "1b47061264138c4ac30d75fd1eb44270",
"plaintext": `"` + plaintextSub + `"`, // must be valid JSON, hence quoted
}

func sanitizeSecretsInObject(obj 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)
}
} else {
copy[k] = v
}
}
return copy
}
Loading
Loading