Skip to content

Commit e3baf42

Browse files
committed
Merge branch 'main' into add-what-do-i-do-if-kosli-is-down-faq
2 parents 38f6e86 + af4d109 commit e3baf42

File tree

8 files changed

+213
-2
lines changed

8 files changed

+213
-2
lines changed

cmd/kosli/attestGeneric.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func newAttestGenericCmd(out io.Writer) *cobra.Command {
126126
return ValidateRegistryFlags(cmd, o.fingerprintOptions)
127127

128128
},
129+
129130
RunE: func(cmd *cobra.Command, args []string) error {
130131
return o.run(args)
131132
},

cmd/kosli/beginTrail_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (suite *BeginTrailCommandTestSuite) TestBeginTrailCmd() {
5151
wantError: true,
5252
name: "beginning a trail with an invalid template fails",
5353
cmd: fmt.Sprintf("begin trail test-123 --flow %s --template-file testdata/invalid_template.yml %s", suite.flowName, suite.defaultKosliArguments),
54-
goldenRegex: "Error: template file is invalid. 1 validation error for Template\n.*",
54+
goldenRegex: "Error: 1 validation error for Template\n.*",
5555
},
5656
{
5757
name: "can begin a trail with a valid template",

cmd/kosli/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
2020
newCreateEnvironmentCmd(out),
2121
newCreateFlowCmd(out),
2222
newCreatePolicyCmd(out),
23+
newCreateAttestationTypeCmd(out),
2324
)
2425
return cmd
2526
}

cmd/kosli/createAttestationType.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
8+
"github.com/kosli-dev/cli/internal/requests"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const createAttestationTypeShortDesc = `Create or update a Kosli attestation type.`
13+
14+
const createAttestationTypeLongDesc = createAttestationTypeShortDesc + `
15+
16+
^TYPE-NAME^ must start with a letter or number, and only contain letters, numbers, ^.^, ^-^, ^_^, and ^~^.
17+
18+
^--schema^ is a path to a file containing a JSON schema which will be used to validate attestations made using this type.
19+
20+
^--jq^ defines the evaluation rules for this attestation type. This can be repeated in order to add additional rules. All rules must return ^true^ for the evaluation to pass.
21+
`
22+
23+
const createAttestationTypeExample = `
24+
kosli create attestation type person-of-age \
25+
--description "Attest that a person meets the age requirements." \
26+
--schema person-schema.json \
27+
--jq ".age >= 18"
28+
--jq ".age < 65"
29+
`
30+
31+
type createAttestationTypeOptions struct {
32+
payload CreateAttestationTypePayload
33+
schemaFilePath string
34+
jqRules []string
35+
}
36+
37+
type JQEvaluatorPayload struct {
38+
ContentType string `json:"content_type"`
39+
Rules []string `json:"rules"`
40+
}
41+
42+
func NewJQEvaluatorPayload(rules []string) *JQEvaluatorPayload {
43+
return &JQEvaluatorPayload{"jq", rules}
44+
}
45+
46+
type CreateAttestationTypePayload struct {
47+
TypeName string `json:"name"`
48+
Description string `json:"description,omitempty"`
49+
Evaluator *JQEvaluatorPayload `json:"evaluator,omitempty"`
50+
}
51+
52+
func newCreateAttestationTypeCmd(out io.Writer) *cobra.Command {
53+
o := new(createAttestationTypeOptions)
54+
cmd := &cobra.Command{
55+
Use: "attestation-type TYPE-NAME",
56+
Short: createAttestationTypeShortDesc,
57+
Long: createAttestationTypeLongDesc,
58+
Example: createAttestationTypeExample,
59+
Args: cobra.ExactArgs(1),
60+
PreRunE: func(cmd *cobra.Command, args []string) error {
61+
err := RequireGlobalFlags(global, []string{"Org", "ApiToken"})
62+
if err != nil {
63+
return ErrorBeforePrintingUsage(cmd, err.Error())
64+
}
65+
return nil
66+
},
67+
RunE: func(cmd *cobra.Command, args []string) error {
68+
return o.run(args)
69+
},
70+
Hidden: true,
71+
}
72+
73+
cmd.Flags().StringVarP(&o.payload.Description, "description", "d", "", attestationTypeDescriptionFlag)
74+
cmd.Flags().StringVarP(&o.schemaFilePath, "schema", "s", "", attestationTypeSchemaFlag)
75+
cmd.Flags().StringArrayVar(&o.jqRules, "jq", []string{}, attestationTypeJqFlag)
76+
77+
addDryRunFlag(cmd)
78+
return cmd
79+
}
80+
81+
func (o *createAttestationTypeOptions) run(args []string) error {
82+
o.payload.TypeName = args[0]
83+
if len(o.jqRules) > 0 {
84+
o.payload.Evaluator = NewJQEvaluatorPayload(o.jqRules)
85+
}
86+
87+
form, err := prepareAttestationTypeForm(o.payload, o.schemaFilePath)
88+
if err != nil {
89+
return err
90+
}
91+
92+
url := fmt.Sprintf("%s/api/v2/custom-attestation-types/%s", global.Host, global.Org)
93+
reqParams := &requests.RequestParams{
94+
Method: http.MethodPost,
95+
URL: url,
96+
Form: form,
97+
DryRun: global.DryRun,
98+
Token: global.ApiToken,
99+
}
100+
_, err = kosliClient.Do(reqParams)
101+
if err == nil && !global.DryRun {
102+
logger.Info("attestation-type %s was created", o.payload.TypeName)
103+
}
104+
return err
105+
}
106+
107+
func prepareAttestationTypeForm(payload interface{}, schemaFilePath string) ([]requests.FormItem, error) {
108+
form, err := newAttestationTypeForm(payload, schemaFilePath)
109+
if err != nil {
110+
return []requests.FormItem{}, err
111+
}
112+
return form, nil
113+
}
114+
115+
// newAttestationTypeForm constructs a list of FormItems for an attestation-type
116+
// form submission.
117+
func newAttestationTypeForm(payload interface{}, schemaFilePath string) (
118+
[]requests.FormItem, error,
119+
) {
120+
form := []requests.FormItem{
121+
{Type: "field", FieldName: "data_json", Content: payload},
122+
}
123+
124+
if schemaFilePath != "" {
125+
form = append(form, requests.FormItem{Type: "file", FieldName: "type_schema", Content: schemaFilePath})
126+
}
127+
128+
return form, nil
129+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/suite"
8+
)
9+
10+
// Define the suite, and absorb the built-in basic suite
11+
// functionality from testify - including a T() method which
12+
// returns the current testing context
13+
type CreateAttestationTypeTestSuite struct {
14+
suite.Suite
15+
defaultKosliArguments string
16+
}
17+
18+
func (suite *CreateAttestationTypeTestSuite) SetupTest() {
19+
global = &GlobalOpts{
20+
ApiToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImNkNzg4OTg5In0.e8i_lA_QrEhFncb05Xw6E_tkCHU9QfcY4OLTVUCHffY",
21+
Org: "docs-cmd-test-user",
22+
Host: "http://localhost:8001",
23+
}
24+
suite.defaultKosliArguments = fmt.Sprintf(" --host %s --org %s --api-token %s", global.Host, global.Org, global.ApiToken)
25+
}
26+
27+
func (suite *CreateAttestationTypeTestSuite) TestCustomAttestationTypeCmd() {
28+
tests := []cmdTestCase{
29+
{
30+
wantError: true,
31+
name: "fails when no arguments are provided",
32+
cmd: "create attestation-type" + suite.defaultKosliArguments,
33+
golden: "Error: accepts 1 arg(s), received 0\n",
34+
},
35+
{
36+
name: "type name is provided",
37+
cmd: "create attestation-type wibble" + suite.defaultKosliArguments,
38+
golden: "attestation-type wibble was created\n",
39+
},
40+
{
41+
name: "type description is provided",
42+
cmd: "create attestation-type wibble-2 --description 'description of attestation type'" + suite.defaultKosliArguments,
43+
golden: "attestation-type wibble-2 was created\n",
44+
},
45+
{
46+
name: "type schema is provided",
47+
cmd: "create attestation-type wibble-4 --schema testdata/person-schema.json" + suite.defaultKosliArguments,
48+
golden: "attestation-type wibble-4 was created\n",
49+
},
50+
{
51+
name: "type jq evaluator is provided",
52+
cmd: `create attestation-type wibble-5 --jq '.age > 21' --jq '.age < 50'` + suite.defaultKosliArguments,
53+
golden: "attestation-type wibble-5 was created\n",
54+
},
55+
{
56+
name: `jq evaluators can include bare "`,
57+
cmd: `create attestation-type wibble-6 --jq '.name | startswith("B")'` + suite.defaultKosliArguments,
58+
golden: "attestation-type wibble-6 was created\n",
59+
},
60+
}
61+
62+
runTestCmd(suite.T(), tests)
63+
}
64+
65+
// In order for 'go test' to run this suite, we need to create
66+
// a normal test function and pass our suite to suite.Run
67+
func TestCreateAttestationTypeTestSuite(t *testing.T) {
68+
suite.Run(t, new(CreateAttestationTypeTestSuite))
69+
}

cmd/kosli/createFlow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (suite *CreateFlowCommandTestSuite) TestCreateFlowCmd() {
8787
wantError: true,
8888
name: "creating a flow with an invalid template fails",
8989
cmd: "create flow newFlowWithTemplate --template-file testdata/invalid_template.yml --description \"my new flow\" " + suite.defaultKosliArguments,
90-
goldenRegex: "Error: template file is invalid. 1 validation error for Template\n.*",
90+
goldenRegex: "Error: Input payload validation failed.*",
9191
},
9292
{
9393
wantError: true,

cmd/kosli/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ The ^.kosli_ignore^ will be treated as part of the artifact like any other file,
237237
sonarRevisionFlag = "[conditional] The revision of the SonarCloud/SonarQube project. Only required if you want to use the project key/revision to get the scan results rather than using Sonar's metadata file and you have overridden the default revision, or you aren't using a CI. Defaults to the value of the git commit flag."
238238
logicalEnvFlag = "[required] The logical environment."
239239
physicalEnvFlag = "[required] The physical environment."
240+
attestationTypeDescriptionFlag = "[optional] The attestation type description."
241+
attestationTypeSchemaFlag = "[optional] Path to the attestation type schema in JSON Schema format."
242+
attestationTypeJqFlag = "[optional] The attestation type evaluation JQ rules."
240243
)
241244

242245
var global *GlobalOpts
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "object",
3+
"additionalProperties": true,
4+
"properties": {
5+
"name": {"type": "string"},
6+
"age": {"type": "integer"}
7+
}
8+
}

0 commit comments

Comments
 (0)