Skip to content

Commit

Permalink
add sensitive type for accesskey
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaji-kharse committed Nov 4, 2024
1 parent 64ff7b8 commit 12dae9d
Show file tree
Hide file tree
Showing 9 changed files with 886 additions and 6,289 deletions.
6 changes: 3 additions & 3 deletions graphql/admin/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func resolveTenantRestore(ctx context.Context, m schema.Mutation) (*resolve.Reso
IncrementalFrom: uint64(input.RestoreInput.IncrementalFrom),
IsPartial: input.RestoreInput.IsPartial,
EncryptionKeyFile: input.RestoreInput.EncryptionKeyFile,
AccessKey: input.RestoreInput.AccessKey,
AccessKey: pb.Sensitive(input.RestoreInput.AccessKey),
SecretKey: input.RestoreInput.SecretKey,
SessionToken: input.RestoreInput.SessionToken,
Anonymous: input.RestoreInput.Anonymous,
Expand Down Expand Up @@ -100,7 +100,7 @@ func resolveRestore(ctx context.Context, m schema.Mutation) (*resolve.Resolved,
IncrementalFrom: uint64(input.IncrementalFrom),
IsPartial: input.IsPartial,
EncryptionKeyFile: input.EncryptionKeyFile,
AccessKey: input.AccessKey,
AccessKey: pb.Sensitive(input.AccessKey),
SecretKey: input.SecretKey,
SessionToken: input.SessionToken,
Anonymous: input.Anonymous,
Expand All @@ -119,7 +119,7 @@ func resolveRestore(ctx context.Context, m schema.Mutation) (*resolve.Resolved,
func restore(ctx context.Context, m schema.Mutation, req pb.RestoreRequest) (*resolve.Resolved, bool) {
wg := &sync.WaitGroup{}
if err := worker.ProcessRestoreRequest(context.Background(), &req, wg); err != nil {
pb.Redact(req.ProtoReflect().Interface())
// pb.Redact(req.ProtoReflect().Interface())
glog.Warningf("error processing restore request: %+v, err: %v", req, err)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to SecretKey
flows to a logging call.
Sensitive data returned by an access to SecretKey
flows to a logging call.
Sensitive data returned by an access to SecretKey
flows to a logging call.
return resolve.DataResult(
m,
Expand Down
4 changes: 2 additions & 2 deletions protos/pb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ message RestoreRequest {
string backup_id = 4;

// Credentials when using a minio or S3 bucket as the backup location.
string access_key = 5 [(dataSensitive) = true];
bytes access_key = 5 [(dataSensitive) = true];
string secret_key = 6 [(dataSensitive) = true];
string session_token = 7;
bool anonymous = 8;
Expand Down Expand Up @@ -769,7 +769,7 @@ message BackupKey {
string attr = 2;
uint64 uid = 3;
uint64 start_uid = 4;
bytes term = 5;
string term = 5;
uint32 count = 6;
uint64 namespace = 7;
}
Expand Down
7,075 changes: 816 additions & 6,259 deletions protos/pb/pb.pb.go

Large diffs are not rendered by default.

41 changes: 22 additions & 19 deletions protos/pb/sensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,27 @@

package pb

import (
"google.golang.org/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
// Sensitive implements the Stringer interface to redact its contents.
// Use this type for sensitive info such as keys, passwords, or secrets so it doesn't leak
// as output such as logs.
type Sensitive []byte

// Redact replaces sensitive string fields with "****"
func Redact(pb proto.Message) {
m := pb.ProtoReflect()
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
// Check for custom option indicating sensitivity
opts := fd.Options().(*descriptorpb.FieldOptions)
if proto.GetExtension(opts, E_DataSensitive).(bool) {
// If the field is a string type, replace the value with "****"
if fd.Kind() == protoreflect.StringKind {
m.Set(fd, protoreflect.ValueOfString("****"))
}
}
return true
})
func (Sensitive) String() string {
return "****"
}

// Redact replaces sensitive string fields with "****"
// func Redact(pb proto.Message) {
// m := pb.ProtoReflect()
// m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
// // Check for custom option indicating sensitivity
// opts := fd.Options().(*descriptorpb.FieldOptions)
// if proto.GetExtension(opts, E_DataSensitive).(bool) {
// // If the field is a string type, replace the value with "****"
// if fd.Kind() == protoreflect.StringKind {
// m.Set(fd, protoreflect.ValueOfString("****"))
// }
// }
// return true
// })
// }
4 changes: 2 additions & 2 deletions worker/backup_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ func FillRestoreCredentials(location string, req *pb.RestoreRequest) error {
}

defaultCreds := credentials.Value{
AccessKeyID: req.AccessKey,
AccessKeyID: string(req.AccessKey),
SecretAccessKey: string(req.SecretKey),
SessionToken: string(req.SessionToken),
}
provider := x.MinioCredentialsProvider(uri.Scheme, defaultCreds)

creds, _ := provider.Retrieve() // Error is always nil.

req.AccessKey = creds.AccessKeyID
req.AccessKey = pb.Sensitive(creds.AccessKeyID)
req.SecretKey = creds.SecretAccessKey
req.SessionToken = creds.SessionToken

Expand Down
6 changes: 3 additions & 3 deletions worker/online_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func ProcessRestoreRequest(ctx context.Context, req *pb.RestoreRequest, wg *sync
}

creds := x.MinioCredentials{
AccessKey: req.AccessKey,
AccessKey: string(req.AccessKey),
SecretKey: req.SecretKey,
SessionToken: req.SessionToken,
Anonymous: req.Anonymous,
Expand Down Expand Up @@ -240,7 +240,7 @@ func handleRestoreProposal(ctx context.Context, req *pb.RestoreRequest, pidx uin

// Reset tablets and set correct tablets to match the restored backup.
creds := &x.MinioCredentials{
AccessKey: req.AccessKey,
AccessKey: string(req.AccessKey),
SecretKey: req.SecretKey,
SessionToken: req.SessionToken,
Anonymous: req.Anonymous,
Expand Down Expand Up @@ -509,7 +509,7 @@ func getEncConfig(req *pb.RestoreRequest) (*viper.Viper, error) {

func getCredentialsFromRestoreRequest(req *pb.RestoreRequest) *x.MinioCredentials {
return &x.MinioCredentials{
AccessKey: req.AccessKey,
AccessKey: string(req.AccessKey),
SecretKey: req.SecretKey,
SessionToken: req.SessionToken,
Anonymous: req.Anonymous,
Expand Down
1 change: 1 addition & 0 deletions worker/restore_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func (m *mapper) processReqCh(ctx context.Context) error {

restoreKey, ns, err := fromBackupKey(kv.Key)
if err != nil {
fmt.Printf("backup key is ---------------->%x\n", kv.Key)
return errors.Wrap(err, "fromBackupKey")
}

Expand Down
36 changes: 36 additions & 0 deletions worker/restore_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package worker

import (
"encoding/hex"
"fmt"
"testing"

"github.com/dgraph-io/dgraph/v24/protos/pb"
"google.golang.org/protobuf/proto"
)

func TestRestore(t *testing.T) {
hexVal := "0802120b417574686f722e6e616d652a210ba7095692826af0b5dbe98d882e509736f7f205006def55d6cf773bae4b7a9b23"

data, err := hex.DecodeString(hexVal)
if err != nil {
t.Fatalf("Error while decoding hex string: %v", err)
}

fmt.Println([]rune(string(data)))

for _, r := range string(data) {
fmt.Println("--------->", r, []byte(string(r)))
}

backupKey := &pb.BackupKey{}
if err := proto.Unmarshal(data, backupKey); err != nil {
panic("error")

// return nil, 0, errors.Wrapf(err, "while reading backup key %s", hex.Dump(key))
}
fmt.Println(string(data))

fmt.Println(fromBackupKey(data))

Check failure on line 34 in worker/restore_map_test.go

View workflow job for this annotation

GitHub Actions / dgraph-oss-build

undefined: fromBackupKey
t.FailNow()
}
2 changes: 1 addition & 1 deletion x/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (p ParsedKey) ToBackupKey() *pb.BackupKey {
key.Attr = attr
key.Uid = p.Uid
key.StartUid = p.StartUid
key.Term = []byte(p.Term)
key.Term = p.Term
key.Count = p.Count

switch {
Expand Down

0 comments on commit 12dae9d

Please sign in to comment.