Skip to content

Commit

Permalink
WKS-1193 - Use dynamic actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ehl-jf committed Nov 26, 2024
1 parent 30c5246 commit 7ac397e
Show file tree
Hide file tree
Showing 94 changed files with 3,103 additions and 2,888 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
go-version-file: go.mod

- name: Static Code Analysis
uses: golangci/golangci-lint-action@v5
uses: golangci/golangci-lint-action@v6
with:
version: latest

Expand All @@ -36,6 +36,9 @@ jobs:
go-version-file: go.mod

- name: Run Gosec Security Scanner
uses: securego/[email protected]
uses: securego/[email protected]
env:
GOSECGOVERSION: go1.23.3
GOTOOLCHAIN: go1.23.3
with:
args: -exclude G204,G301,G302,G304,G306,G601,G101 -tests -exclude-dir \.*test\.* ./...
args: -exclude G204,G301,G302,G304,G306,G601,G101,G407 -tests -exclude-dir \.*test\.* ./...
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.tools
bin
*-nogit*
*-nogit*
**/mocks/*
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ linters:
disable-all: true
enable:
- errcheck
- exportloopref
- copyloopvar
# - depguard
# - gci
- gofumpt
Expand All @@ -19,7 +19,6 @@ linters:
- govet
- ineffassign
- makezero
- megacheck
- misspell
- noctx
- nolintlint
Expand Down
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GOIMPORTS:
########## ANALYSE ##########

GOLANGCI_LINT = ${TOOLS_DIR}/golangci-lint
GOLANGCI_LINT_VERSION = 1.55.2
GOLANGCI_LINT_VERSION = 1.62.2

verify: GOLANGCI_LINT
echo $(GO_SOURCES)
Expand All @@ -59,7 +59,17 @@ build-install:: build

########## TEST ##########

test-prereq: prereq
.PHONY: clean-mock
clean-mock:
@echo Cleaning generated mock files
find . -path "*/mocks/*.go" -delete

.PHONY: generate-mock
generate-mock: clean-mock
@echo Generating test mocks
go generate ./...

test-prereq: prereq generate-mock
mkdir -p target/reports

test: PACKAGES=./...
Expand Down
14 changes: 8 additions & 6 deletions commands/add_secret_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"

"github.com/jfrog/jfrog-cli-platform-services/commands/common"

plugins_common "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
Expand Down Expand Up @@ -38,12 +40,12 @@ func GetAddSecretCommand() components.Command {
}

func (c *addSecretCommand) run() error {
manifest, err := model.ReadManifest()
manifest, err := common.ReadManifest()
if err != nil {
return err
}

if err = manifest.Validate(); err != nil {
if err = common.ValidateManifest(manifest, nil); err != nil {
return err
}

Expand All @@ -57,7 +59,7 @@ func (c *addSecretCommand) run() error {
return err
}

encryptionKey, err := model.ReadSecretPassword()
encryptionKey, err := common.ReadSecretPassword()
if err != nil {
return err
}
Expand All @@ -67,7 +69,7 @@ func (c *addSecretCommand) run() error {
return err
}

encryptedValue, err := model.EncryptSecret(encryptionKey, secretValue)
encryptedValue, err := common.EncryptSecret(encryptionKey, secretValue)
if err != nil {
return err
}
Expand All @@ -78,7 +80,7 @@ func (c *addSecretCommand) run() error {
existingEncryptedSecrets[k] = v
}

if err = manifest.DecryptSecrets(encryptionKey); err != nil {
if err = common.DecryptManifestSecrets(manifest, encryptionKey); err != nil {
log.Debug("Cannot decrypt existing secrets: %+v", err)
return fmt.Errorf("others secrets are encrypted with a different password, please use the same one")
} else {
Expand All @@ -91,7 +93,7 @@ func (c *addSecretCommand) run() error {
manifest.Secrets[secretName] = encryptedValue
}

err = manifest.Save()
err = common.SaveManifest(manifest)
if err != nil {
return err
}
Expand Down
37 changes: 22 additions & 15 deletions commands/add_secret_cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//go:build test
// +build test

package commands

import (
"fmt"
"os"
"testing"

"github.com/jfrog/jfrog-cli-platform-services/commands/common"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -28,10 +33,10 @@ func TestAddSecretCmd(t *testing.T) {
name: "add",
secretName: "sec-1",
secretValue: "val-1",
secretPassword: secretPassword,
secretPassword: common.SecretPassword,
patchManifest: func(mf *model.Manifest) {
mf.Secrets = model.Secrets{
"sec-2": mustEncryptSecret(t, "val-2"),
"sec-2": common.MustEncryptSecret(t, "val-2"),
}
},
assert: assertSecrets(model.Secrets{
Expand All @@ -43,7 +48,7 @@ func TestAddSecretCmd(t *testing.T) {
name: "add with nil manifest",
secretName: "sec-1",
secretValue: "val-1",
secretPassword: secretPassword,
secretPassword: common.SecretPassword,
patchManifest: func(mf *model.Manifest) {
mf.Secrets = nil
},
Expand All @@ -55,21 +60,21 @@ func TestAddSecretCmd(t *testing.T) {
name: "add with different password",
secretName: "sec-1",
secretValue: "val-1",
secretPassword: secretPassword,
secretPassword: common.SecretPassword,
patchManifest: func(mf *model.Manifest) {
mf.Secrets["sec-2"] = mustEncryptSecret(t, "val-2", "other-password")
mf.Secrets["sec-2"] = common.MustEncryptSecret(t, "val-2", "other-password")
},
wantErr: "others secrets are encrypted with a different password, please use the same one",
},
{
name: "edit secret",
secretName: "sec-1",
secretValue: "val-1",
secretPassword: secretPassword,
secretPassword: common.SecretPassword,
commandArgs: []string{fmt.Sprintf("--%s", model.FlagEdit)},
patchManifest: func(mf *model.Manifest) {
mf.Secrets = model.Secrets{
"sec-1": mustEncryptSecret(t, "val-1-before"),
"sec-1": common.MustEncryptSecret(t, "val-1-before"),
}
},
assert: assertSecrets(model.Secrets{"sec-1": "val-1"}),
Expand All @@ -78,10 +83,10 @@ func TestAddSecretCmd(t *testing.T) {
name: "fails if the secret exists",
secretName: "sec-1",
secretValue: "val-1",
secretPassword: secretPassword,
secretPassword: common.SecretPassword,
patchManifest: func(mf *model.Manifest) {
mf.Secrets = model.Secrets{
"sec-1": mustEncryptSecret(t, "val-1-before"),
"sec-1": common.MustEncryptSecret(t, "val-1-before"),
}
},
wantErr: "sec-1 already exists, use --edit to overwrite",
Expand All @@ -94,15 +99,17 @@ func TestAddSecretCmd(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
workerDir, workerName := prepareWorkerDirForTest(t)
common.NewMockWorkerServer(t, common.NewServerStub(t).WithDefaultActionsMetadataEndpoint())

workerDir, workerName := common.PrepareWorkerDirForTest(t)

runCmd := createCliRunner(t, GetInitCommand(), GetAddSecretCommand())
runCmd := common.CreateCliRunner(t, GetInitCommand(), GetAddSecretCommand())

err := runCmd("worker", "init", "GENERIC_EVENT", workerName)
require.NoError(t, err)

if tt.patchManifest != nil {
patchManifest(t, tt.patchManifest)
common.PatchManifest(t, tt.patchManifest)
}

if tt.secretPassword != "" {
Expand All @@ -121,7 +128,7 @@ func TestAddSecretCmd(t *testing.T) {
})
}

manifestBefore, err := model.ReadManifest(workerDir)
manifestBefore, err := common.ReadManifest(workerDir)
require.NoError(t, err)

cmd := []string{"worker", "add-secret"}
Expand All @@ -135,7 +142,7 @@ func TestAddSecretCmd(t *testing.T) {

if tt.wantErr == "" {
require.NoError(t, err)
manifestAfter, err := model.ReadManifest(workerDir)
manifestAfter, err := common.ReadManifest(workerDir)
assert.NoError(t, err)
tt.assert(t, manifestBefore, manifestAfter)
} else {
Expand All @@ -148,7 +155,7 @@ func TestAddSecretCmd(t *testing.T) {
func assertSecrets(wantSecrets model.Secrets) addSecretAssertFunc {
return func(t *testing.T, manifestBefore, manifestAfter *model.Manifest) {
require.Equalf(t, len(wantSecrets), len(manifestAfter.Secrets), "Invalid secrets length")
require.NoError(t, manifestAfter.DecryptSecrets())
require.NoError(t, common.DecryptManifestSecrets(manifestAfter))
assert.Equalf(t, wantSecrets, manifestAfter.Secrets, "Secrets mismatch")
}
}
71 changes: 0 additions & 71 deletions commands/cmd_input_reader.go

This file was deleted.

Loading

0 comments on commit 7ac397e

Please sign in to comment.