-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: main
Are you sure you want to change the base?
Changes from 14 commits
1eff76d
662c6e6
c9c477f
90836fb
15ab22d
ff6f17f
eca8f62
51df1d2
cb158c2
9bea208
7b01c06
c969593
38ba44c
2d91073
4298179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} |
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 ? 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. Its a WIP. This has to move to cli part to make it work again with flags. |
||
// "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.
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 😄
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.
Yes backward compatibility on the tracing. But with the side note that we now do not filter anymore but use configuration of the
EnvironmentCapturer