Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Datree
Commits
726ad2e6
Unverified
Commit
726ad2e6
authored
3 years ago
by
Roy Hadad
Committed by
GitHub
3 years ago
Browse files
Options
Download
Email Patches
Plain Diff
fix: make anonymous invocations fail with --policy flag (#559)
parent
f6662ea3
No related merge requests found
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
bl/evaluation/evaluator_test.go
+3
-3
bl/evaluation/evaluator_test.go
bl/policy/policy_factory.go
+6
-3
bl/policy/policy_factory.go
bl/policy/policy_factory_test.go
+16
-4
bl/policy/policy_factory_test.go
cmd/test/main.go
+1
-1
cmd/test/main.go
cmd/test/main_test.go
+3
-3
cmd/test/main_test.go
internal/fixtures/policyAsCode/prerun.json
+3
-1
internal/fixtures/policyAsCode/prerun.json
with
32 additions
and
15 deletions
+32
-15
bl/evaluation/evaluator_test.go
+
3
-
3
View file @
726ad2e6
...
...
@@ -121,7 +121,7 @@ func TestEvaluate(t *testing.T) {
}
prerunData
:=
mockGetPreRunData
()
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
)
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
,
prerunData
.
RegistrationURL
)
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
...
...
@@ -174,7 +174,7 @@ func request_evaluation_all_valid() *evaluateTestCase {
validFilePath
:=
"internal/fixtures/kube/pass-all.yaml"
prerunData
:=
mockGetPreRunData
()
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
)
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
,
prerunData
.
RegistrationURL
)
return
&
evaluateTestCase
{
name
:
"should request validation without invalid files"
,
...
...
@@ -222,7 +222,7 @@ func request_evaluation_all_valid() *evaluateTestCase {
func
request_evaluation_all_invalid
()
*
evaluateTestCase
{
prerunData
:=
mockGetPreRunData
()
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
)
policy
,
_
:=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
,
prerunData
.
RegistrationURL
)
return
&
evaluateTestCase
{
name
:
"should not request validation if there are no valid files"
,
...
...
This diff is collapsed.
Click to expand it.
bl/policy/policy_factory.go
+
6
-
3
View file @
726ad2e6
...
...
@@ -22,7 +22,11 @@ type RuleWithSchema struct {
MessageOnFailure
string
}
func
CreatePolicy
(
policies
*
cliClient
.
EvaluationPrerunPolicies
,
policyName
string
)
(
Policy
,
error
)
{
func
CreatePolicy
(
policies
*
cliClient
.
EvaluationPrerunPolicies
,
policyName
string
,
registrationURL
string
)
(
Policy
,
error
)
{
if
policies
==
nil
&&
policyName
!=
""
&&
policyName
!=
"Default"
{
return
Policy
{},
fmt
.
Errorf
(
"policy %s doesn't exist, sign in to the dashboard to customize your policies: %s"
,
policyName
,
registrationURL
)
}
defaultRules
,
err
:=
internal_policy
.
GetDefaultRules
()
if
err
!=
nil
{
...
...
@@ -46,8 +50,7 @@ func CreatePolicy(policies *cliClient.EvaluationPrerunPolicies, policyName strin
}
if
chosenPolicy
==
nil
{
err
:=
fmt
.
Errorf
(
"policy %s doesn't exist"
,
policyName
)
return
Policy
{},
err
return
Policy
{},
fmt
.
Errorf
(
"policy %s doesn't exist"
,
policyName
)
}
rules
,
err
=
populateRules
(
chosenPolicy
.
Rules
,
policies
.
CustomRules
,
defaultRules
.
Rules
)
...
...
This diff is collapsed.
Click to expand it.
bl/policy/policy_factory_test.go
+
16
-
4
View file @
726ad2e6
...
...
@@ -2,6 +2,7 @@ package policy
import
(
"encoding/json"
"fmt"
"os"
"testing"
...
...
@@ -17,7 +18,7 @@ import (
const
policiesJsonPath
=
"../../internal/fixtures/policyAsCode/prerun.json"
func
TestCreatePolicy
(
t
*
testing
.
T
)
{
p
oliciesJson
:=
mockGetPreRunData
()
p
reRunData
:=
mockGetPreRunData
()
err
:=
os
.
Chdir
(
"../../"
)
if
err
!=
nil
{
...
...
@@ -25,7 +26,7 @@ func TestCreatePolicy(t *testing.T) {
}
t
.
Run
(
"Test Create Policy With Default Policy"
,
func
(
t
*
testing
.
T
)
{
policy
,
_
:=
CreatePolicy
(
p
oliciesJson
.
PoliciesJson
,
""
)
policy
,
_
:=
CreatePolicy
(
p
reRunData
.
PoliciesJson
,
""
,
preRunData
.
RegistrationURL
)
var
expectedRules
[]
RuleWithSchema
defaultRules
,
err
:=
internal_policy
.
GetDefaultRules
()
...
...
@@ -58,7 +59,7 @@ func TestCreatePolicy(t *testing.T) {
})
t
.
Run
(
"Test Create Policy With Specific Policy"
,
func
(
t
*
testing
.
T
)
{
policy
,
err
:=
CreatePolicy
(
p
oliciesJson
.
PoliciesJson
,
"labels_best_practices2"
)
policy
,
err
:=
CreatePolicy
(
p
reRunData
.
PoliciesJson
,
"labels_best_practices2"
,
preRunData
.
RegistrationURL
)
var
expectedRules
[]
RuleWithSchema
if
err
!=
nil
{
...
...
@@ -80,7 +81,7 @@ func TestCreatePolicy(t *testing.T) {
})
t
.
Run
(
"Test Create Policy With Custom Rules"
,
func
(
t
*
testing
.
T
)
{
policy
,
err
:=
CreatePolicy
(
p
oliciesJson
.
PoliciesJson
,
"labels_best_practices3"
)
policy
,
err
:=
CreatePolicy
(
p
reRunData
.
PoliciesJson
,
"labels_best_practices3"
,
preRunData
.
RegistrationURL
)
var
expectedRules
[]
RuleWithSchema
if
err
!=
nil
{
panic
(
err
)
...
...
@@ -97,6 +98,17 @@ func TestCreatePolicy(t *testing.T) {
assert
.
Equal
(
t
,
expectedRules
,
policy
.
Rules
)
})
t
.
Run
(
"Test Create Policy for anonymous user with --policy flag Default"
,
func
(
t
*
testing
.
T
)
{
_
,
err
:=
CreatePolicy
(
nil
,
"Default"
,
preRunData
.
RegistrationURL
)
assert
.
Equal
(
t
,
nil
,
err
)
})
t
.
Run
(
"Test Create Policy for anonymous user with --policy flag not default"
,
func
(
t
*
testing
.
T
)
{
policy
,
err
:=
CreatePolicy
(
nil
,
"my-policy"
,
preRunData
.
RegistrationURL
)
assert
.
Equal
(
t
,
fmt
.
Errorf
(
"policy my-policy doesn't exist, sign in to the dashboard to customize your policies: %s"
,
preRunData
.
RegistrationURL
),
err
)
assert
.
Equal
(
t
,
Policy
{},
policy
)
})
}
func
mockGetPreRunData
()
*
cliClient
.
EvaluationPrerunDataResponse
{
...
...
This diff is collapsed.
Click to expand it.
cmd/test/main.go
+
1
-
1
View file @
726ad2e6
...
...
@@ -262,7 +262,7 @@ func GenerateTestCommandData(testCommandFlags *TestCommandFlags, localConfigCont
policies
=
evaluationPrerunDataResp
.
PoliciesJson
}
policy
,
err
:=
policy_factory
.
CreatePolicy
(
policies
,
testCommandFlags
.
PolicyName
)
policy
,
err
:=
policy_factory
.
CreatePolicy
(
policies
,
testCommandFlags
.
PolicyName
,
evaluationPrerunDataResp
.
RegistrationURL
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
cmd/test/main_test.go
+
3
-
3
View file @
726ad2e6
...
...
@@ -326,7 +326,7 @@ func test_all_k8s_resources_tested() *TestFlowTestCase {
validK8sFilesConfigurations
=
append
(
validK8sFilesConfigurations
,
fileConfigurations
)
}
preRunData
:=
mockGetPreRunData
()
policy
,
_
:=
policy_factory
.
CreatePolicy
(
preRunData
.
PoliciesJson
,
""
)
policy
,
_
:=
policy_factory
.
CreatePolicy
(
preRunData
.
PoliciesJson
,
""
,
preRunData
.
RegistrationURL
)
close
(
invalidFilesChan
)
return
&
TestFlowTestCase
{
...
...
@@ -461,7 +461,7 @@ func test_all_k8s_resources_tested() *TestFlowTestCase {
func
test_no_k8s_resources_found
()
*
TestFlowTestCase
{
root
:=
pathFromRoot
(
"internal/fixtures/nonKube/"
)
preRunData
:=
mockGetPreRunData
()
policy
,
_
:=
policy_factory
.
CreatePolicy
(
preRunData
.
PoliciesJson
,
""
)
policy
,
_
:=
policy_factory
.
CreatePolicy
(
preRunData
.
PoliciesJson
,
""
,
preRunData
.
RegistrationURL
)
paths
:=
[]
string
{
root
+
"/docker-compose-config.yaml"
,
root
+
"/simple.json"
,
root
+
"/simple.yaml"
}
filesConfigurationsChan
:=
make
(
chan
*
extractor
.
FileConfigurations
,
3
)
go
func
()
{
...
...
@@ -683,7 +683,7 @@ func setup() {
FilesExtractor
:
filesExtractorMock
,
}
testingPolicy
,
_
=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
)
testingPolicy
,
_
=
policy_factory
.
CreatePolicy
(
prerunData
.
PoliciesJson
,
""
,
prerunData
.
RegistrationURL
)
}
func
TestTestCommandFlagsValidation
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
internal/fixtures/policyAsCode/prerun.json
+
3
-
1
View file @
726ad2e6
...
...
@@ -91,5 +91,7 @@
]
},
"accountExists"
:
true
,
"isPolicyAsCodeMode"
:
false
"isPolicyAsCodeMode"
:
false
,
"registrationURL"
:
"https://app.datree.io/login?t=gfdgdsteasgfsdgfdsfsd"
,
"promptRegistrationURL"
:
"https://app.datree.io/promptLogin?t=gfdgdsteasgfsdgfdsfsd"
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help