-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize exported stack state by scrubbing secrets (#107)
When upgrade tests persist stack.json, it contains secrets, making it unsuitable for committing. This PR adds a sanitization step that scrubs secrets, as identified by the p/p signature, from the state. Fixes #106 --------- Co-authored-by: Daniel Bradley <[email protected]>
- Loading branch information
1 parent
cecfbc7
commit c0ea332
Showing
7 changed files
with
559 additions
and
5 deletions.
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.