-
Notifications
You must be signed in to change notification settings - Fork 19
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
chore: New environment variable obfuscation functionality #355
Open
matglas
wants to merge
14
commits into
in-toto:main
Choose a base branch
from
matglas:env-obfuscate
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+617
−113
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1eff76d
chore: New environment variable obfuscation functionality
matglas 662c6e6
Update attestation/environment/obfuscate.go
matglas c9c477f
feat: Change to default obfuscate and add cli flags.
matglas 90836fb
chore: Adjust naming to filter envs. Fix tests.
matglas 15ab22d
chore: Making the environment test cases more robust.
matglas ff6f17f
feat: Add exclude-sensitive-key flag.
matglas eca8f62
Update attestation/environment/sensitive_env_vars.go
matglas 51df1d2
chore: Fix tests and command run.
matglas cb158c2
chore: Fix linux tracing in commandrun.
matglas 9bea208
Refactor environment.
matglas 7b01c06
Add new environment to commandrun
matglas c969593
Add license headers
matglas 38ba44c
Fix lint errors
matglas 2d91073
WIP Linux tracing fix
matglas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2024 The Witness Contributors | ||
// | ||
// 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 attestation | ||
|
||
import ( | ||
env "github.com/in-toto/go-witness/environment" | ||
) | ||
|
||
func (ctx *AttestationContext) EnvironmentCapturer() *env.Capture { | ||
return ctx.environmentCapturer | ||
} | ||
|
||
// WithEnvFilterVarsEnabled will make the filter (removing) of vars the acting behavior. | ||
// The default behavior is obfuscation of variables. | ||
func WithEnvFilterVarsEnabled() AttestationContextOption { | ||
return func(a *AttestationContext) { | ||
env.WithFilterVarsEnabled()(a.environmentCapturer) | ||
} | ||
} | ||
|
||
// WithEnvAdditionalKeys add additional keys to final list that is checked for sensitive variables. | ||
func WithEnvAdditionalKeys(additionalKeys []string) AttestationContextOption { | ||
return func(a *AttestationContext) { | ||
env.WithAdditionalKeys(additionalKeys)(a.environmentCapturer) | ||
} | ||
} | ||
|
||
// WithEnvExcludeKeys add additional keys to final list that is checked for sensitive variables. | ||
func WithEnvExcludeKeys(excludeKeys []string) AttestationContextOption { | ||
return func(a *AttestationContext) { | ||
env.WithExcludeKeys(excludeKeys)(a.environmentCapturer) | ||
} | ||
} | ||
|
||
// WithEnvDisableDefaultSensitiveList will disable the default list and only use the additional keys. | ||
func WithEnvDisableDefaultSensitiveList() AttestationContextOption { | ||
return func(a *AttestationContext) { | ||
env.WithDisableDefaultSensitiveList()(a.environmentCapturer) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,7 +18,6 @@ import ( | |
"os" | ||
"os/user" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/in-toto/go-witness/attestation" | ||
"github.com/invopop/jsonschema" | ||
|
@@ -35,6 +34,8 @@ const ( | |
var ( | ||
_ attestation.Attestor = &Attestor{} | ||
_ EnvironmentAttestor = &Attestor{} | ||
// defaultFilterSensitiveVarsEnabled = false | ||
// defaultDisableSensitiveVarsDefault = false | ||
) | ||
|
||
type EnvironmentAttestor interface { | ||
|
@@ -47,9 +48,65 @@ type EnvironmentAttestor interface { | |
} | ||
|
||
func init() { | ||
attestation.RegisterAttestation(Name, Type, RunType, func() attestation.Attestor { | ||
return New() | ||
}) | ||
attestation.RegisterAttestation(Name, Type, RunType, func() attestation.Attestor { return New() }) | ||
|
||
// registry.BoolConfigOption( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry if I'm missing some context, is there a reason why this is commented out at the moment @matglas ? |
||
// "filter-sensitive-vars", | ||
// "Switch from obfuscate to filtering variables which removes them from the output completely.", | ||
// defaultFilterSensitiveVarsEnabled, | ||
// func(a attestation.Attestor, filterSensitiveVarsEnabled bool) (attestation.Attestor, error) { | ||
// envAttestor, ok := a.(*Attestor) | ||
// if !ok { | ||
// return a, fmt.Errorf("unexpected attestor type: %T is not a environment attestor", a) | ||
// } | ||
|
||
// envCapture.WithFilterVarsEnabled()(envAttestor.capture) | ||
// return envAttestor, nil | ||
// }, | ||
// ), | ||
// registry.BoolConfigOption( | ||
// "disable-default-sensitive-vars", | ||
// "Disable the default list of sensitive vars and only use the items mentioned by --attestor-environment-sensitive-key.", | ||
// defaultDisableSensitiveVarsDefault, | ||
// func(a attestation.Attestor, disableSensitiveVarsDefault bool) (attestation.Attestor, error) { | ||
// envAttestor, ok := a.(*Attestor) | ||
// if !ok { | ||
// return a, fmt.Errorf("unexpected attestor type: %T is not a environment attestor", a) | ||
// } | ||
|
||
// envCapture.WithDisableDefaultSensitiveList()(envAttestor.capture) | ||
// return envAttestor, nil | ||
// }, | ||
// ), | ||
// registry.StringSliceConfigOption( | ||
// "add-sensitive-key", | ||
// "Add keys or globs (e.g. '*TEXT') to the list of sensitive environment keys.", | ||
// []string{}, | ||
// func(a attestation.Attestor, additionalKeys []string) (attestation.Attestor, error) { | ||
// envAttestor, ok := a.(*Attestor) | ||
// if !ok { | ||
// return a, fmt.Errorf("unexpected attestor type: %T is not a environment attestor", a) | ||
// } | ||
|
||
// envCapture.WithAdditionalKeys(additionalKeys)(envAttestor.capture) | ||
// return envAttestor, nil | ||
// }, | ||
// ), | ||
// registry.StringSliceConfigOption( | ||
// "exclude-sensitive-key", | ||
// "Exclude specific keys from the list of sensitive environment keys. Note: This does not support globs.", | ||
// []string{}, | ||
// func(a attestation.Attestor, excludeKeys []string) (attestation.Attestor, error) { | ||
// envAttestor, ok := a.(*Attestor) | ||
// if !ok { | ||
// return a, fmt.Errorf("unexpected attestor type: %T is not a environment attestor", a) | ||
// } | ||
|
||
// envCapture.WithExcludeKeys(excludeKeys)(envAttestor.capture) | ||
// return envAttestor, nil | ||
// }, | ||
// ), | ||
|
||
} | ||
|
||
type Attestor struct { | ||
|
@@ -58,21 +115,22 @@ type Attestor struct { | |
Username string `json:"username"` | ||
Variables map[string]string `json:"variables,omitempty"` | ||
|
||
blockList map[string]struct{} | ||
osEnviron func() []string | ||
} | ||
|
||
type Option func(*Attestor) | ||
|
||
func WithBlockList(blockList map[string]struct{}) Option { | ||
// WithCustomEnv will override the default os.Environ() method. This could be used to mock. | ||
func WithCustomEnv(osEnviron func() []string) Option { | ||
return func(a *Attestor) { | ||
a.blockList = blockList | ||
a.osEnviron = osEnviron | ||
} | ||
} | ||
|
||
func New(opts ...Option) *Attestor { | ||
attestor := &Attestor{ | ||
blockList: DefaultBlockList(), | ||
} | ||
attestor := &Attestor{} | ||
|
||
attestor.osEnviron = os.Environ | ||
|
||
for _, opt := range opts { | ||
opt(attestor) | ||
|
@@ -109,25 +167,11 @@ func (a *Attestor) Attest(ctx *attestation.AttestationContext) error { | |
a.Username = user.Username | ||
} | ||
|
||
FilterEnvironmentArray(os.Environ(), a.blockList, func(key, val, _ string) { | ||
a.Variables[key] = val | ||
}) | ||
a.Variables = ctx.EnvironmentCapturer().Capture(a.osEnviron()) | ||
|
||
return nil | ||
} | ||
|
||
func (a *Attestor) Data() *Attestor { | ||
return a | ||
} | ||
|
||
// splitVariable splits a string representing an environment variable in the format of | ||
// "KEY=VAL" and returns the key and val separately. | ||
func splitVariable(v string) (key, val string) { | ||
parts := strings.SplitN(v, "=", 2) | ||
key = parts[0] | ||
if len(parts) > 1 { | ||
val = parts[1] | ||
} | ||
|
||
return | ||
} |
This file was deleted.
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.
Just as a sanity check, we're ensuring backwards compatibility here right? Again, been out of the loop on this last week or two but just asking the questions to ensure that this was all considered 😄