-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix serialization of service variable when encrypted text is used (#114)
* Fix serialization of service variable when encrypted text is used * Fix application test
- Loading branch information
1 parent
de7525d
commit 91a0d0d
Showing
8 changed files
with
182 additions
and
70 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cac | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type SecretRef struct { | ||
Name string | ||
} | ||
|
||
func (r *SecretRef) MarshalYAML() (interface{}, error) { | ||
if (r == &SecretRef{}) { | ||
return []byte{}, nil | ||
} | ||
|
||
if r.Name == "" { | ||
return nil, errors.New("name must be set") | ||
} | ||
|
||
return fmt.Sprintf("secretName:%s", r.Name), nil | ||
} | ||
|
||
func (r *SecretRef) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
|
||
var val interface{} | ||
err := unmarshal(&val) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
value := val.(string) | ||
|
||
parts := strings.Split(value, ":") | ||
|
||
if len(parts) == 1 { | ||
r.Name = parts[0] | ||
} else if len(parts) == 2 { | ||
r.Name = parts[1] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *SecretRef) String() string { | ||
return fmt.Sprintf("secretName:%s", r.Name) | ||
} |
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,30 @@ | ||
package cac | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/harness/harness-go-sdk/harness/utils" | ||
) | ||
|
||
type Service struct { | ||
HarnessApiVersion HarnessApiVersion `yaml:"harnessApiVersion" json:"harnessApiVersion"` | ||
Type ObjectType `yaml:"type" json:"type"` | ||
Id string `yaml:"-"` | ||
Name string `yaml:"-"` | ||
ArtifactType ArtifactType `yaml:"artifactType,omitempty"` | ||
DeploymentType DeploymentType `yaml:"deploymentType,omitempty"` | ||
Description string `yaml:"description,omitempty"` | ||
Tags map[string]string `yaml:"tags,omitempty"` | ||
HelmVersion HelmVersion `yaml:"helmVersion,omitempty"` | ||
ApplicationId string `yaml:"-"` | ||
DeploymentTypeTemplateUri string `yaml:"deploymentTypeTemplateUri,omitempty"` | ||
ConfigVariables []*ServiceVariable `yaml:"configVariables,omitempty"` | ||
} | ||
|
||
func (a *Service) IsEmpty() bool { | ||
return reflect.DeepEqual(a, &Service{}) | ||
} | ||
|
||
func (s *Service) Validate() (bool, error) { | ||
return utils.RequiredStringFieldsSet(s, []string{"ApplicationId"}) | ||
} |
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,57 @@ | ||
package cac | ||
|
||
import ( | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type ServiceVariable struct { | ||
Name string `yaml:"name,omitempty"` | ||
Value string `yaml:"value,omitempty"` | ||
ValueType VariableValueType `yaml:"valueType,omitempty"` | ||
} | ||
|
||
// We need to customize the marshaling of this object because of the way the `Value` | ||
// field works. When the ValueType is ENCRYPTED_TEXT the value needs to be secretName:<secretName>. | ||
|
||
func (s *ServiceVariable) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
|
||
type Alias ServiceVariable | ||
|
||
aux := &struct { | ||
*Alias | ||
}{ | ||
Alias: (*Alias)(s), | ||
} | ||
|
||
if err := unmarshal(&aux.Alias); err != nil { | ||
return err | ||
} | ||
|
||
if s.ValueType == VariableOverrideValueTypes.Text { | ||
s.Value = aux.Value | ||
} else if s.ValueType == VariableOverrideValueTypes.EncryptedText { | ||
ref := &SecretRef{} | ||
if err := yaml.Unmarshal([]byte(aux.Value), ref); err != nil { | ||
return err | ||
} | ||
|
||
s.Value = ref.Name | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *ServiceVariable) MarshalYAML() (interface{}, error) { | ||
var aux ServiceVariable = *r | ||
|
||
if r.ValueType == VariableOverrideValueTypes.Text { | ||
aux.Value = r.Value | ||
} else if r.ValueType == VariableOverrideValueTypes.EncryptedText { | ||
ref := &SecretRef{ | ||
Name: r.Value, | ||
} | ||
aux.Value = ref.String() | ||
} | ||
|
||
return aux, nil | ||
} |
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