|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package cmdelasticsearchkeystore |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + cmdutil "github.com/elastic/ecctl/cmd/util" |
| 25 | + "github.com/elastic/ecctl/pkg/deployment/elasticsearch" |
| 26 | + "github.com/elastic/ecctl/pkg/ecctl" |
| 27 | +) |
| 28 | + |
| 29 | +const setKeystoreLong = `Manages the keystore settings of an Elasticsearch cluster. |
| 30 | +Note that each operation is add/modify only, unspecified existing keystore values will be unchanged.` |
| 31 | + |
| 32 | +var setKeystoreExamples = ` |
| 33 | +$ cat keystore_example.json |
| 34 | +{ |
| 35 | + "secrets": { |
| 36 | + "s3.client.foobar.access_key": { |
| 37 | + "value": "AKIXAIQFKXPHIFXSILUWPA", |
| 38 | + "as_file": false |
| 39 | + }, |
| 40 | + "s3.client.foobar.secret_key": { |
| 41 | + "value": "18qXOpY2zGlApay1237dLXh+LG1X5LUNWjTHq5X1SWjf++m+p0" |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +$ ecctl deployment elasticsearch keystore set 4c052fb17f65467a9b3c36d060106377 --file keystore_example.json |
| 46 | +{ |
| 47 | + "secrets": { |
| 48 | + "s3.client.foobar.access_key": { |
| 49 | + "as_file": false |
| 50 | + }, |
| 51 | + "s3.client.foobar.secret_key": { |
| 52 | + "as_file": false |
| 53 | + } |
| 54 | + } |
| 55 | +}`[1:] |
| 56 | + |
| 57 | +var setCmd = &cobra.Command{ |
| 58 | + Use: `set <cluster id> -f <file definition.json>`, |
| 59 | + Short: "Updates an Elasticsearch cluster keystore with the contents of a file", |
| 60 | + Long: setKeystoreLong, |
| 61 | + Example: setKeystoreExamples, |
| 62 | + PreRunE: cmdutil.MinimumNArgsAndUUID(1), |
| 63 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 64 | + filename, _ := cmd.Flags().GetString("file") |
| 65 | + var req models.KeystoreContents |
| 66 | + if err := cmdutil.DecodeFile(filename, &req); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + res, err := elasticsearch.SetKeystore(elasticsearch.SetKeystoreParams{ |
| 71 | + API: ecctl.Get().API, |
| 72 | + ClusterID: args[0], |
| 73 | + Request: &req, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + return ecctl.Get().Formatter.Format("", res) |
| 80 | + }, |
| 81 | +} |
| 82 | + |
| 83 | +func init() { |
| 84 | + Command.AddCommand(setCmd) |
| 85 | + setCmd.Flags().StringP("file", "f", "", "JSON file that contains JSON-style domain-specific keystore definition") |
| 86 | + setCmd.MarkFlagRequired("file") |
| 87 | + setCmd.MarkFlagFilename("file", "*.json") |
| 88 | +} |
0 commit comments