Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions service/deployed_version/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/release-argus/Argus/service/shared"
"github.com/release-argus/Argus/util"
logutil "github.com/release-argus/Argus/util/log"
)
Expand All @@ -33,6 +34,7 @@ func Refresh(
previousType string,
overrides *string,
semanticVersioning *string, // nil, "true", "false", "null" (unchanged, true, false, default).
secretRefs *shared.VSecretRef,
) (string, error) {
if lookup == nil {
return "", errors.New("lookup is nil")
Expand Down Expand Up @@ -63,6 +65,7 @@ func Refresh(
if err != nil {
return "", err
}
newLookup.InheritSecrets(lookup, secretRefs)
} else if previousType == "manual" && newLookup.GetType() == "manual" &&
overrides != nil {
if err := json.Unmarshal([]byte(*overrides), &lookup); err != nil {
Expand Down
90 changes: 79 additions & 11 deletions service/deployed_version/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/release-argus/Argus/service/dashboard"
"github.com/release-argus/Argus/service/deployed_version/types/manual"
"github.com/release-argus/Argus/service/deployed_version/types/web"
"github.com/release-argus/Argus/service/shared"
"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
Expand All @@ -39,14 +40,15 @@ func TestRefresh(t *testing.T) {
}

type versions struct {
latestVersion string
deployedVersion string
latestVersion *string
deployedVersion *string
deployedVersionTimestamp string
}
type args struct {
overrides *string
semanticVersioning *string
version versions
secretRefs shared.VSecretRef
}

// GIVEN a Lookup and various JSON strings to override parts of it.
Expand Down Expand Up @@ -114,8 +116,8 @@ func TestRefresh(t *testing.T) {
"Refresh new version": {
args: args{
version: versions{
latestVersion: testVersion,
deployedVersion: "0.0.0",
latestVersion: &testVersion,
deployedVersion: test.StringPtr("0.0.0"),
deployedVersionTimestamp: time.Now().UTC().Add(-4 * time.Hour).Format(time.RFC3339)},
},
previous: testLookup("url", false),
Expand All @@ -126,15 +128,80 @@ func TestRefresh(t *testing.T) {
"Refresh new version that's newer than latest": {
args: args{
version: versions{
latestVersion: "0.0.0",
deployedVersion: "0.0.0",
latestVersion: test.StringPtr("0.0.0"),
deployedVersion: test.StringPtr("0.0.0"),
deployedVersionTimestamp: time.Now().UTC().Add(-4 * time.Hour).Format(time.RFC3339)},
},
previous: testLookup("url", false),
errRegex: `^$`,
want: testVersion,
announce: 1,
},
"InheritSecrets inherits header secrets": {
args: args{
overrides: test.StringPtr(test.TrimJSON(`{
"headers": [
{
"key": "` + test.LookupWithHeaderAuth["header_key"] + `",
"value": "` + util.SecretValue + `",
"old_index": 0
}
]
}`)),
version: versions{
latestVersion: &testVersion,
deployedVersion: test.StringPtr("0.0.0"),
deployedVersionTimestamp: time.Now().UTC().Add(-4 * time.Hour).Format(time.RFC3339),
}},
previous: test.IgnoreError(t, func() (Lookup, error) {
l := testLookup("url", false)
lTyped, _ := l.(*web.Lookup)
lTyped.Method = "POST"
lTyped.Headers = shared.Headers{{
Key: test.LookupWithHeaderAuth["header_key"],
Value: test.LookupWithHeaderAuth["header_value_pass"],
}}
return lTyped, nil
}),
want: testVersion,
announce: 0,
errRegex: `^$`,
},
"InheritSecrets can be overridden with non '<secret>' values": {
args: args{
overrides: test.StringPtr(test.TrimJSON(`{
"headers": [
{
"key": "` + test.LookupWithHeaderAuth["header_key"] + `",
"value": "` + test.LookupWithHeaderAuth["header_value_fail"] + `",
"old_index": 0
}
]
}`)),
secretRefs: shared.VSecretRef{
Headers: []shared.OldIntIndex{
{OldIndex: test.IntPtr(0)}},
},
version: versions{
latestVersion: test.StringPtr(""),
deployedVersion: test.StringPtr(""),
deployedVersionTimestamp: time.Now().UTC().Add(-4 * time.Hour).Format(time.RFC3339),
}},
previous: test.IgnoreError(t, func() (Lookup, error) {
l := testLookup("url", false)
lTyped, _ := l.(*web.Lookup)
lTyped.Method = "POST"
lTyped.URL = test.LookupWithHeaderAuth["url_valid"]
lTyped.Headers = shared.Headers{{
Key: test.LookupWithHeaderAuth["header_key"],
Value: test.LookupWithHeaderAuth["header_value_pass"],
}}
return lTyped, nil
}),
want: "",
announce: 0,
errRegex: `Hook rules were not satisfied\.`,
},
}

for name, tc := range tests {
Expand All @@ -158,14 +225,14 @@ func TestRefresh(t *testing.T) {
name, "", "",
&dashboard.Options{})
// Set the latest version.
if tc.args.version.latestVersion != "" {
if tc.args.version.latestVersion != nil {
targetStatus.SetLatestVersion(
tc.args.version.latestVersion, "",
*tc.args.version.latestVersion, "",
false)
}
if tc.args.version.deployedVersion != "" {
if tc.args.version.deployedVersion != nil {
targetStatus.SetDeployedVersion(
tc.args.version.deployedVersion, tc.args.version.deployedVersionTimestamp,
*tc.args.version.deployedVersion, tc.args.version.deployedVersionTimestamp,
false)
}
previousStatus = targetStatus.Copy(true)
Expand All @@ -179,7 +246,8 @@ func TestRefresh(t *testing.T) {
got, err := Refresh(
tc.previous,
previousType, tc.args.overrides,
tc.args.semanticVersioning)
tc.args.semanticVersioning,
&tc.args.secretRefs)

// THEN we get an error if expected.
if tc.errRegex != "" || err != nil {
Expand Down
2 changes: 1 addition & 1 deletion service/deployed_version/types/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ func (l *Lookup) Query(_ bool, _ logutil.LogFrom) error {
}

// InheritSecrets will inherit secrets from the `otherLookup`.
func (l *Lookup) InheritSecrets(otherLookup Interface, secretRefs *shared.DVSecretRef) {
func (l *Lookup) InheritSecrets(otherLookup Interface, secretRefs *shared.VSecretRef) {
// Nothing to inherit.
}
2 changes: 1 addition & 1 deletion service/deployed_version/types/base/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestInheritSecrets(t *testing.T) {
otherLookup := &testLookup{
Lookup: Lookup{
Type: "other"}}
secretRefs := &shared.DVSecretRef{
secretRefs := &shared.VSecretRef{
Headers: []shared.OldIntIndex{
{OldIndex: test.IntPtr(0)}},
}
Expand Down
2 changes: 1 addition & 1 deletion service/deployed_version/types/base/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Interface interface {
Query(metrics bool, logFrom logutil.LogFrom) (err error)

// InheritSecrets will inherit secrets from the `otherLookup`.
InheritSecrets(otherLookup Interface, secretRefs *shared.DVSecretRef)
InheritSecrets(otherLookup Interface, secretRefs *shared.VSecretRef)

// Helpers:

Expand Down
4 changes: 3 additions & 1 deletion service/deployed_version/types/web/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ func (l *Lookup) httpRequest(logFrom logutil.LogFrom) ([]byte, error) {
}

// getVersion returns the latest version from `body` that matches the URLCommands, and Regex requirements.
func (l *Lookup) getVersion(body []byte, logFrom logutil.LogFrom) (version string, err error) {
func (l *Lookup) getVersion(body []byte, logFrom logutil.LogFrom) (string, error) {
var version string
// If JSON is provided, use it to extract the version.
if l.JSON != "" {
var err error
version, err = util.GetValueByKey(body, l.JSON, l.url())
if err != nil {
logutil.Log.Error(err, logFrom, true)
Expand Down
45 changes: 10 additions & 35 deletions service/deployed_version/types/web/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type Lookup struct {
AllowInvalidCerts *bool `json:"allow_invalid_certs,omitempty" yaml:"allow_invalid_certs,omitempty"` // Default - false = Disallows invalid HTTPS certificates.
TargetHeader string `json:"target_header,omitempty" yaml:"target_header,omitempty"` // OPTIONAL: header to target for the version.

BasicAuth *BasicAuth `json:"basic_auth,omitempty" yaml:"basic_auth,omitempty"` // OPTIONAL: basic auth credentials.
Headers []Header `json:"headers,omitempty" yaml:"headers,omitempty"` // OPTIONAL: request headers.
Body string `json:"body,omitempty" yaml:"body,omitempty"` // OPTIONAL: request body.
JSON string `json:"json,omitempty" yaml:"json,omitempty"` // OPTIONAL: JSON key to use e.g. version_current.
Regex string `json:"regex,omitempty" yaml:"regex,omitempty"` // OPTIONAL: regex for the version.
RegexTemplate string `json:"regex_template,omitempty" yaml:"regex_template,omitempty"` // OPTIONAL: template to apply to the RegEx match.
BasicAuth *BasicAuth `json:"basic_auth,omitempty" yaml:"basic_auth,omitempty"` // OPTIONAL: basic auth credentials.
Headers shared.Headers `json:"headers,omitempty" yaml:"headers,omitempty"` // OPTIONAL: request headers.
Body string `json:"body,omitempty" yaml:"body,omitempty"` // OPTIONAL: request body.
JSON string `json:"json,omitempty" yaml:"json,omitempty"` // OPTIONAL: JSON key to use e.g. version_current.
Regex string `json:"regex,omitempty" yaml:"regex,omitempty"` // OPTIONAL: regex for the version.
RegexTemplate string `json:"regex_template,omitempty" yaml:"regex_template,omitempty"` // OPTIONAL: template to apply to the RegEx match.
}

// New returns a new Lookup from a string in a given format (json/yaml).
Expand Down Expand Up @@ -107,42 +107,17 @@ type BasicAuth struct {
Password string `json:"password" yaml:"password"`
}

// Header to use in the HTTP request.
type Header struct {
Key string `json:"key" yaml:"key"` // Header key, e.g. X-Sig.
Value string `json:"value" yaml:"value"` // Value to give the key.
}

// inheritSecrets from the `oldLookup`.
func (l *Lookup) InheritSecrets(otherLookup base.Interface, secretRefs *shared.DVSecretRef) {
// InheritSecrets will inherit secrets from the `otherLookup`.
func (l *Lookup) InheritSecrets(otherLookup base.Interface, secretRefs *shared.VSecretRef) {
if otherL, ok := otherLookup.(*Lookup); ok {
if l.BasicAuth != nil &&
l.BasicAuth.Password == util.SecretValue &&
otherL.BasicAuth != nil {
l.BasicAuth.Password = otherL.BasicAuth.Password
}

// If we have headers in old and new.
if len(l.Headers) != 0 &&
len(otherL.Headers) != 0 {
for i := range l.Headers {
// If referencing a secret of an existing header.
if l.Headers[i].Value == util.SecretValue {
// Don't have a secretRef for this header.
if i >= len(secretRefs.Headers) {
break
}
oldIndex := secretRefs.Headers[i].OldIndex
// Not a reference to an old Header.
if oldIndex == nil {
continue
}

if *oldIndex < len(otherL.Headers) {
l.Headers[i].Value = otherL.Headers[*oldIndex].Value
}
}
}
if secretRefs != nil {
l.Headers.InheritSecrets(otherL.Headers, secretRefs.Headers)
}
}
}
Loading
Loading