|
| 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 | +} |
0 commit comments