From 319f2c0935cdfb15f23ad09c2a9e7999ead5effd Mon Sep 17 00:00:00 2001 From: srinandan <13950006+srinandan@users.noreply.github.com> Date: Mon, 27 Nov 2023 08:34:42 -0800 Subject: [PATCH] feat: onboard sec profile create --- cmd/securityprofiles/create.go | 52 +++++++++++++++++++ cmd/securityprofiles/securityprofiles.go | 1 + .../securityprofiles/securityprofiles.go | 32 ++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 cmd/securityprofiles/create.go diff --git a/cmd/securityprofiles/create.go b/cmd/securityprofiles/create.go new file mode 100644 index 000000000..1571ec7e2 --- /dev/null +++ b/cmd/securityprofiles/create.go @@ -0,0 +1,52 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package securityprofiles + +import ( + "internal/apiclient" + "internal/client/securityprofiles" + + "github.com/apigee/apigeecli/cmd/utils" + "github.com/spf13/cobra" +) + +// CreateCmd to get a securityprofile +var CreateCmd = &cobra.Command{ + Use: "create", + Short: "Create a new Security Profile", + Long: "Create a new Security Profile", + Args: func(cmd *cobra.Command, args []string) (err error) { + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + content, err := utils.ReadFile(securityActionFile) + if err != nil { + return err + } + _, err = securityprofiles.Create(name, content) + return + }, +} + +var securityActionFile string + +func init() { + CreateCmd.Flags().StringVarP(&name, "name", "n", + "", "Security Action name") + CreateCmd.Flags().StringVarP(&securityActionFile, "file", "f", + "", "Path to a file containing Security Profile content") + _ = CreateCmd.MarkFlagRequired("name") + _ = CreateCmd.MarkFlagRequired("file") +} diff --git a/cmd/securityprofiles/securityprofiles.go b/cmd/securityprofiles/securityprofiles.go index 9d47d76da..05b7bb97d 100644 --- a/cmd/securityprofiles/securityprofiles.go +++ b/cmd/securityprofiles/securityprofiles.go @@ -37,6 +37,7 @@ func init() { Cmd.AddCommand(DeleteCmd) Cmd.AddCommand(AttachCmd) Cmd.AddCommand(DetachCmd) + Cmd.AddCommand(CreateCmd) _ = Cmd.MarkFlagRequired("org") } diff --git a/internal/client/securityprofiles/securityprofiles.go b/internal/client/securityprofiles/securityprofiles.go index 0214dbced..fe1d3ab6e 100644 --- a/internal/client/securityprofiles/securityprofiles.go +++ b/internal/client/securityprofiles/securityprofiles.go @@ -15,6 +15,7 @@ package securityprofiles import ( + "encoding/json" "net/url" "path" "strconv" @@ -23,8 +24,39 @@ import ( "internal/apiclient" ) +type secprofile struct { + Name string `json:"name"` + DisplayName string `json:"displayName"` + Description string `json:"description,omitempty"` + ProfileConfig profileConfig `json:"profileConfig"` + ScoreConfigs []scoreConfig `json:"scoreConfigs,omitempty"` +} + +type profileConfig struct { + Categories []category `json:"categories"` +} + +type scoreConfig struct { + Title string `json:"title,omitempty"` + Description string `json:"description,omitempty"` + ScorePath string `json:"scorePath,omitempty"` +} + +type category struct { + Abuse interface{} `json:"abuse,omitempty"` + Mediation interface{} `json:"mediation,omitempty"` + Authorization interface{} `json:"authorization,omitempty"` + Threat interface{} `json:"threat,omitempty"` + Mtls interface{} `json:"mtls,omitempty"` + Cors interface{} `json:"cors,omitempty"` +} + // Create func Create(name string, content []byte) (respBody []byte, err error) { + sc := secprofile{} + if err = json.Unmarshal(content, &sc); err != nil { + return nil, err + } u, _ := url.Parse(apiclient.BaseURL) u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "securityProfiles") q := u.Query()