fix: [CDE-530]: use value for MaskSecret type (#3054)

* fix lint
* add non nil Mask secret
* add comment
* add non nil Mask secret
pull/3597/head
Deepak Bhatt 2024-11-27 06:53:13 +00:00 committed by Harness
parent 3a3c8e2c85
commit 7765cf0d2f
2 changed files with 17 additions and 14 deletions

View File

@ -43,14 +43,14 @@ type (
// by an automated login process. // by an automated login process.
Credentials struct { Credentials struct {
Email string Email string
Name *types.MaskSecret Name types.MaskSecret
Password *types.MaskSecret Password types.MaskSecret
} }
ResolvedCredentials struct { ResolvedCredentials struct {
Branch string Branch string
// CloneURL contains credentials for private repositories in url prefix // CloneURL contains credentials for private repositories in url prefix
CloneURL *types.MaskSecret CloneURL types.MaskSecret
Credentials *Credentials Credentials *Credentials
RepoName string RepoName string
} }

View File

@ -29,13 +29,13 @@ type MaskSecret struct {
hashedValue string hashedValue string
} }
func NewMaskSecret(val string) *MaskSecret { func NewMaskSecret(val string) MaskSecret {
hash := sha256.New() hash := sha256.New()
hash.Write([]byte(val)) hash.Write([]byte(val))
hashedValueStr := fmt.Sprintf("%x", hash.Sum(nil)) hashedValueStr := fmt.Sprintf("%x", hash.Sum(nil))
return &MaskSecret{ return MaskSecret{
value: val, value: val,
hashedValue: hashedValueStr[:maxTruncatedLen], hashedValue: hashedValueStr[:maxTruncatedLen],
} }
@ -43,25 +43,28 @@ func NewMaskSecret(val string) *MaskSecret {
// Value returns the unmasked value of the MaskSecret. // Value returns the unmasked value of the MaskSecret.
// Use cautiously to avoid exposing sensitive data. // Use cautiously to avoid exposing sensitive data.
func (s *MaskSecret) Value() string { func (s MaskSecret) Value() string {
if s == nil {
return ""
}
return s.value return s.value
} }
func (s *MaskSecret) String() string { func (s MaskSecret) String() string {
if s == nil { if s.hashedValue == "" && s.value != "" {
return "" // this case can arise when MarkSecret is created by UnmarshalJSON func where we do not
} // use NewMaskSecret constructor.
hash := sha256.New()
hash.Write([]byte(s.value))
hashedValueStr := fmt.Sprintf("%x", hash.Sum(nil))
s.hashedValue = hashedValueStr[:maxTruncatedLen]
}
return s.hashedValue return s.hashedValue
} }
func (s *MaskSecret) MarshalJSON() ([]byte, error) { func (s MaskSecret) MarshalJSON() ([]byte, error) {
return json.Marshal(s.value) return json.Marshal(s.value)
} }
// UnmarshalJSON needs pointer receiver as it modify the receiver.
func (s *MaskSecret) UnmarshalJSON(data []byte) error { func (s *MaskSecret) UnmarshalJSON(data []byte) error {
var input string var input string
if err := json.Unmarshal(data, &input); err != nil { if err := json.Unmarshal(data, &input); err != nil {