-
Notifications
You must be signed in to change notification settings - Fork 14
Implement redacting of fields for MPA, apply it to httpoverrpc #562
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
Open
sfc-gh-mwalas
wants to merge
6
commits into
main
Choose a base branch
from
mwalas-httpoverrpc-mpa
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69833b2
Implement redacting of certain fields for MPA
sfc-gh-mwalas 489ab4f
Remove excessive printing
sfc-gh-mwalas b885275
Rollback a hack for local testing
sfc-gh-mwalas 2211c50
Make sure redaction does not fail on nil messages
sfc-gh-mwalas ac361e0
Rollback sansshell-server default rego policy change
sfc-gh-mwalas 4934876
update copyright year
sfc-gh-mwalas 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,22 @@ | ||
/* Copyright (c) 2025 Snowflake Inc. All rights reserved. | ||
|
||
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 mpa defines the RPC interface for the sansshell MPA actions. | ||
package annotations | ||
|
||
// To regenerate the proto headers if the .proto changes, just run go generate | ||
// and this encodes the necessary magic: | ||
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative --go-grpcproxy_out=. --go-grpcproxy_opt=paths=source_relative mpa_annotations.proto |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,27 @@ | ||
/* Copyright (c) 2019 Snowflake Inc. All rights reserved. | ||
|
||
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. | ||
*/ | ||
|
||
syntax = "proto3"; | ||
|
||
package sansshell.annotations; | ||
|
||
import "google/protobuf/descriptor.proto"; | ||
|
||
option go_package = "github.com/Snowflake-Labs/sansshell/services/mpa/annotations"; | ||
|
||
extend google.protobuf.FieldOptions { | ||
bool mpa_redacted = 50000; // Using a high number to avoid conflicts | ||
} |
This file contains hidden or 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,117 @@ | ||
/* Copyright (c) 2025 Snowflake Inc. All rights reserved. | ||
|
||
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 mpahooks | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Snowflake-Labs/sansshell/services/mpa/annotations" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/types/descriptorpb" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
// RedactFieldsForMPA processes an Any proto message and redacts (sets to nil/zero) | ||
// any fields that are marked with the mpa_redacted annotation. | ||
// Returns true if the message was modified. | ||
func RedactFieldsForMPA(anyMsg *anypb.Any) (bool, error) { | ||
if anyMsg == nil { | ||
return false, nil | ||
} | ||
// First extract the message from Any | ||
msg, err := anyMsg.UnmarshalNew() | ||
if err != nil { | ||
return false, fmt.Errorf("failed to unmarshal Any message: %v", err) | ||
} | ||
|
||
// Process the message to redact marked fields | ||
modified := redactMessageFields(msg) | ||
|
||
// If we made changes, update the Any message | ||
if modified { | ||
if err := anyMsg.MarshalFrom(msg); err != nil { | ||
return false, fmt.Errorf("failed to re-marshal message: %v", err) | ||
} | ||
} | ||
|
||
return modified, nil | ||
} | ||
|
||
// redactMessageFields recursively processes a message and redacts fields | ||
// marked with the mpa_redacted annotation. | ||
// Returns true if any field was redacted. | ||
func redactMessageFields(message proto.Message) bool { | ||
modified := false | ||
|
||
// Get the reflective view of the message | ||
m := message.ProtoReflect() | ||
|
||
// Iterate through all fields in the message | ||
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { | ||
// Check if this field has the mpa_redacted option set | ||
opts := fd.Options().(*descriptorpb.FieldOptions) | ||
|
||
if proto.GetExtension(opts, annotations.E_MpaRedacted).(bool) { | ||
m.Clear(fd) | ||
modified = true | ||
return true // Continue iteration | ||
} | ||
|
||
// If it's a message field that's not nil, recursively process it | ||
if fd.Kind() == protoreflect.MessageKind && v.IsValid() { | ||
if fd.IsList() { | ||
// Handle repeated message fields | ||
list := v.List() | ||
for i := 0; i < list.Len(); i++ { | ||
item := list.Get(i) | ||
if item.Message().IsValid() { | ||
// Create a new proto.Message from this item | ||
nestedMsg := item.Message().Interface() | ||
if redactMessageFields(nestedMsg) { | ||
modified = true | ||
} | ||
} | ||
} | ||
} else if fd.IsMap() { | ||
// Handle map fields where values are messages | ||
if fd.MapValue().Kind() == protoreflect.MessageKind { | ||
mapVal := v.Map() | ||
mapVal.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool { | ||
if v.Message().IsValid() { | ||
nestedMsg := v.Message().Interface() | ||
if redactMessageFields(nestedMsg) { | ||
modified = true | ||
} | ||
} | ||
return true | ||
}) | ||
} | ||
} else if v.Message().IsValid() { | ||
// Handle regular message fields | ||
nestedMsg := v.Message().Interface() | ||
if redactMessageFields(nestedMsg) { | ||
modified = true | ||
} | ||
} | ||
} | ||
|
||
return true // Continue iteration | ||
}) | ||
|
||
return modified | ||
} |
This file contains hidden or 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 hidden or 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
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.
update copyright year