forked from jfrog/jfrog-cli-security
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuration_test.go
More file actions
139 lines (126 loc) · 4.87 KB
/
curation_test.go
File metadata and controls
139 lines (126 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jfrog/jfrog-cli-security/commands/curation"
securityTests "github.com/jfrog/jfrog-cli-security/tests"
securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils"
"github.com/jfrog/jfrog-cli-security/tests/utils/integration"
commonCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/common/format"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
)
func TestCurationAudit(t *testing.T) {
integration.InitCurationTest(t)
tempDirPath, cleanUp := securityTestUtils.CreateTestProjectEnvAndChdir(t, filepath.Join(filepath.FromSlash(securityTests.GetTestResourcesPath()), "projects", "package-managers", "npm"))
defer cleanUp()
expectedRequest := map[string]bool{
"/api/npm/npms/json/-/json-9.0.6.tgz": false,
"/api/npm/npms/xml/-/xml-1.0.1.tgz": false,
}
requestToFail := map[string]bool{
"/api/npm/npms/xml/-/xml-1.0.1.tgz": false,
}
serverMock, config := curationServer(t, expectedRequest, requestToFail)
cleanUpJfrogHome, err := coreTests.SetJfrogHome()
assert.NoError(t, err)
defer cleanUpJfrogHome()
config.User = "admin"
config.Password = "password"
config.ServerId = "test"
configCmd := commonCommands.NewConfigCommand(commonCommands.AddOrEdit, config.ServerId).SetDetails(config).SetUseBasicAuthOnly(true).SetInteractive(false)
assert.NoError(t, configCmd.Run())
defer serverMock.Close()
// Create build config
assert.NoError(t, commonCommands.CreateBuildConfigWithOptions(false, project.Npm,
commonCommands.WithResolverServerId(config.ServerId),
commonCommands.WithResolverRepo("npms"),
commonCommands.WithDeployerServerId(config.ServerId),
commonCommands.WithDeployerRepo("npm-local"),
))
localXrayCli := securityTests.PlatformCli.WithoutCredentials()
workingDirsFlag := fmt.Sprintf("--working-dirs=%s", filepath.Join(tempDirPath, "npm"))
output := localXrayCli.RunCliCmdWithOutput(t, "curation-audit", "--format="+string(format.Json), workingDirsFlag)
expectedResp := getCurationExpectedResponse(config)
var got []curation.PackageStatus
bracketIndex := strings.Index(output, "[")
require.Less(t, 0, bracketIndex, "Unexpected Curation output with missing '['")
err = json.Unmarshal([]byte(output[bracketIndex:]), &got)
assert.NoError(t, err)
assert.Equal(t, expectedResp, got)
for k, v := range expectedRequest {
assert.Truef(t, v, "didn't receive expected GET request for package url %s", k)
}
}
func getCurationExpectedResponse(config *config.ServerDetails) []curation.PackageStatus {
expectedResp := []curation.PackageStatus{
{
Action: "blocked",
PackageName: "xml",
PackageVersion: "1.0.1",
BlockedPackageUrl: config.ArtifactoryUrl + "api/npm/npms/xml/-/xml-1.0.1.tgz",
BlockingReason: curation.BlockingReasonPolicy,
ParentName: "xml",
ParentVersion: "1.0.1",
DepRelation: "direct",
PkgType: "npm",
Policy: []curation.Policy{
{
Policy: "pol1",
Condition: "cond1",
Explanation: "explanation",
Recommendation: "recommendation",
},
{
Policy: "pol2",
Condition: "cond2",
Explanation: "explanation2",
Recommendation: "recommendation2",
},
},
},
}
return expectedResp
}
func curationServer(t *testing.T, expectedRequest map[string]bool, requestToFail map[string]bool) (*httptest.Server, *config.ServerDetails) {
mapLockReadWrite := sync.Mutex{}
serverMock, config, _ := commonTests.CreateRtRestsMockServer(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodHead {
mapLockReadWrite.Lock()
if _, exist := expectedRequest[r.RequestURI]; exist {
expectedRequest[r.RequestURI] = true
}
mapLockReadWrite.Unlock()
if _, exist := requestToFail[r.RequestURI]; exist {
w.WriteHeader(http.StatusForbidden)
}
}
if r.Method == http.MethodGet {
if r.RequestURI == "/api/system/version" {
_, err := w.Write([]byte(`{"version": "7.0.0"}`))
require.NoError(t, err)
w.WriteHeader(http.StatusOK)
return
}
if _, exist := requestToFail[r.RequestURI]; exist {
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{\n \"errors\": [\n {\n \"status\": 403,\n " +
"\"message\": \"Package download was blocked by JFrog Packages " +
"Curation service due to the following policies violated {pol1, cond1, explanation, recommendation}, {pol2, cond2, explanation2, recommendation2}\"\n }\n ]\n}"))
require.NoError(t, err)
}
}
})
return serverMock, config
}