-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): make kv plugin builtin. (#75)
* feat(plugin): make kv plugin builtin. * doc(changelog): update. * chore(ci): raise go runtime to 1.16.9 and 1.17.2
- Loading branch information
Showing
21 changed files
with
7,170 additions
and
3,774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/consul/api" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
|
||
"github.com/elastic/harp/pkg/kv/consul" | ||
"github.com/elastic/harp/pkg/sdk/cmdutil" | ||
"github.com/elastic/harp/pkg/sdk/log" | ||
"github.com/elastic/harp/pkg/tasks/from" | ||
) | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
type fromConsulParams struct { | ||
outputPath string | ||
basePaths []string | ||
lastPathItemAsSecret bool | ||
} | ||
|
||
var fromConsulCmd = func() *cobra.Command { | ||
var params fromConsulParams | ||
|
||
cmd := &cobra.Command{ | ||
Use: "consul", | ||
Short: "Extract KV pairs from Hashicorp Consul KV Store", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Initialize logger and context | ||
ctx, cancel := cmdutil.Context(cmd.Context(), "harp-kv-from-consul", conf.Debug.Enable, conf.Instrumentation.Logs.Level) | ||
defer cancel() | ||
|
||
runFromConsul(ctx, ¶ms) | ||
}, | ||
} | ||
|
||
// Add parameters | ||
cmd.Flags().StringVar(¶ms.outputPath, "out", "-", "Container output path ('-' for stdout)") | ||
cmd.Flags().StringSliceVar(¶ms.basePaths, "paths", []string{}, "Exported base paths") | ||
cmd.Flags().BoolVarP(¶ms.lastPathItemAsSecret, "last-path-item-as-secret-key", "k", false, "Use the last path element as secret key") | ||
|
||
return cmd | ||
} | ||
|
||
func runFromConsul(ctx context.Context, params *fromConsulParams) { | ||
// Create Consul client config from environment. | ||
config := api.DefaultConfig() | ||
|
||
// Creates a new client | ||
client, err := api.NewClient(config) | ||
if err != nil { | ||
log.For(ctx).Fatal("unable to connect to consul cluster", zap.Error(err)) | ||
return | ||
} | ||
|
||
// Delegate to task | ||
t := &from.ExtractKVTask{ | ||
Store: consul.Store(client), | ||
ContainerWriter: cmdutil.FileWriter(params.outputPath), | ||
BasePaths: params.basePaths, | ||
LastPathItemAsSecretKey: params.lastPathItemAsSecret, | ||
} | ||
|
||
// Run the task | ||
if err := t.Run(ctx); err != nil { | ||
log.For(ctx).Fatal("unable to execute kv extract task", zap.Error(err)) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cmd | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.uber.org/zap" | ||
|
||
"github.com/elastic/harp/pkg/kv/etcd3" | ||
"github.com/elastic/harp/pkg/sdk/cmdutil" | ||
"github.com/elastic/harp/pkg/sdk/log" | ||
"github.com/elastic/harp/pkg/sdk/tlsconfig" | ||
"github.com/elastic/harp/pkg/tasks/from" | ||
) | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
type fromEtcd3Params struct { | ||
outputPath string | ||
basePaths []string | ||
lastPathItemAsSecret bool | ||
|
||
endpoints []string | ||
dialTimeout time.Duration | ||
username string | ||
password string | ||
|
||
useTLS bool | ||
caFile string | ||
certFile string | ||
keyFile string | ||
passphrase string | ||
insecureSkipVerify bool | ||
} | ||
|
||
var fromEtcd3Cmd = func() *cobra.Command { | ||
var params fromEtcd3Params | ||
|
||
cmd := &cobra.Command{ | ||
Use: "etcd3", | ||
Short: "Extract KV pairs from CoreOS Etcdv3 KV Store", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Initialize logger and context | ||
ctx, cancel := cmdutil.Context(cmd.Context(), "harp-kv-from-etcdv3", conf.Debug.Enable, conf.Instrumentation.Logs.Level) | ||
defer cancel() | ||
|
||
runFromEtcd3(ctx, ¶ms) | ||
}, | ||
} | ||
|
||
// Add parameters | ||
cmd.Flags().StringVar(¶ms.outputPath, "out", "-", "Container output path ('-' for stdout)") | ||
cmd.Flags().StringSliceVar(¶ms.basePaths, "paths", []string{}, "Exported base paths") | ||
cmd.Flags().BoolVarP(¶ms.lastPathItemAsSecret, "last-path-item-as-secret-key", "k", false, "Use the last path element as secret key") | ||
|
||
cmd.Flags().StringArrayVar(¶ms.endpoints, "endpoints", []string{"http://localhost:2379"}, "Etcd cluster endpoints") | ||
cmd.Flags().DurationVar(¶ms.dialTimeout, "dial-timeout", 15*time.Second, "Etcd cluster dial timeout") | ||
cmd.Flags().StringVar(¶ms.username, "username", "", "Etcd cluster connection username") | ||
cmd.Flags().StringVar(¶ms.password, "password", "", "Etcd cluster connection password") | ||
|
||
cmd.Flags().BoolVar(¶ms.useTLS, "tls", false, "Enable TLS") | ||
cmd.Flags().StringVar(¶ms.caFile, "ca-file", "", "TLS CA Certificate file path") | ||
cmd.Flags().StringVar(¶ms.certFile, "cert-file", "", "TLS Client certificate file path") | ||
cmd.Flags().StringVar(¶ms.keyFile, "key-file", "", "TLS Client private key file path") | ||
cmd.Flags().StringVar(¶ms.passphrase, "key-passphrase", "", "TLS Client private key passphrase") | ||
cmd.Flags().BoolVar(¶ms.insecureSkipVerify, "insecure-skip-verify", false, "Disable TLS certificate verification") | ||
|
||
return cmd | ||
} | ||
|
||
func runFromEtcd3(ctx context.Context, params *fromEtcd3Params) { | ||
// Create config | ||
config := clientv3.Config{ | ||
Context: ctx, | ||
Endpoints: params.endpoints, | ||
DialTimeout: params.dialTimeout, | ||
Username: params.username, | ||
Password: params.password, | ||
} | ||
|
||
if params.useTLS { | ||
tlsConfig, err := tlsconfig.Client(&tlsconfig.Options{ | ||
InsecureSkipVerify: params.insecureSkipVerify, | ||
CAFile: params.caFile, | ||
CertFile: params.certFile, | ||
KeyFile: params.keyFile, | ||
Passphrase: params.passphrase, | ||
}) | ||
if err != nil { | ||
log.For(ctx).Fatal("unable to initialize TLS settings", zap.Error(err)) | ||
return | ||
} | ||
|
||
// Assign TLS settings | ||
config.TLS = tlsConfig | ||
} | ||
|
||
// Creates a new client | ||
client, err := clientv3.New(config) | ||
if err != nil { | ||
log.For(ctx).Fatal("unable to connect to etcdv3 cluster", zap.Error(err)) | ||
return | ||
} | ||
|
||
// Delegate to task | ||
t := &from.ExtractKVTask{ | ||
Store: etcd3.Store(client), | ||
ContainerWriter: cmdutil.FileWriter(params.outputPath), | ||
BasePaths: params.basePaths, | ||
LastPathItemAsSecretKey: params.lastPathItemAsSecret, | ||
} | ||
|
||
// Run the task | ||
if err := t.Run(ctx); err != nil { | ||
log.For(ctx).Fatal("unable to execute kv extract task", zap.Error(err)) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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 cmd | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
zk "github.com/samuel/go-zookeeper/zk" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
|
||
"github.com/elastic/harp/pkg/kv/zookeeper" | ||
"github.com/elastic/harp/pkg/sdk/cmdutil" | ||
"github.com/elastic/harp/pkg/sdk/log" | ||
"github.com/elastic/harp/pkg/tasks/from" | ||
) | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
type fromZookeeperParams struct { | ||
outputPath string | ||
basePaths []string | ||
lastPathItemAsSecret bool | ||
|
||
endpoints []string | ||
dialTimeout time.Duration | ||
} | ||
|
||
var fromZookeeperCmd = func() *cobra.Command { | ||
var params fromZookeeperParams | ||
cmd := &cobra.Command{ | ||
Use: "zookeeper", | ||
Aliases: []string{"zk"}, | ||
Short: "Extract KV pairs from Apache Zookeeper KV Store", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Initialize logger and context | ||
ctx, cancel := cmdutil.Context(cmd.Context(), "harp-kv-from-zookeeper", conf.Debug.Enable, conf.Instrumentation.Logs.Level) | ||
defer cancel() | ||
|
||
runFromZookeeper(ctx, ¶ms) | ||
}, | ||
} | ||
|
||
// Add parameters | ||
cmd.Flags().StringArrayVar(¶ms.endpoints, "endpoints", []string{"127.0.0.1:2181"}, "Zookeeper client endpoints") | ||
cmd.Flags().DurationVar(¶ms.dialTimeout, "dial-timeout", 15*time.Second, "Zookeeper client dial timeout") | ||
cmd.Flags().BoolVarP(¶ms.lastPathItemAsSecret, "last-path-item-as-secret-key", "k", false, "Use the last path element as secret key") | ||
|
||
return cmd | ||
} | ||
|
||
func runFromZookeeper(ctx context.Context, params *fromZookeeperParams) { | ||
// Create config | ||
client, _, err := zk.Connect(params.endpoints, params.dialTimeout) | ||
if err != nil { | ||
log.For(ctx).Fatal("unable to connect to zookeeper cluster", zap.Error(err)) | ||
return | ||
} | ||
|
||
// Delegate to task | ||
t := &from.ExtractKVTask{ | ||
Store: zookeeper.Store(client), | ||
ContainerWriter: cmdutil.FileWriter(params.outputPath), | ||
BasePaths: params.basePaths, | ||
LastPathItemAsSecretKey: params.lastPathItemAsSecret, | ||
} | ||
|
||
// Run the task | ||
if err := t.Run(ctx); err != nil { | ||
log.For(ctx).Fatal("unable to execute kv extract task", zap.Error(err)) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## harp from consul | ||
|
||
Extract KV pairs from Hashicorp Consul KV Store | ||
|
||
``` | ||
harp from consul [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for consul | ||
-k, --last-path-item-as-secret-key Use the last path element as secret key | ||
--out string Container output path ('-' for stdout) (default "-") | ||
--paths strings Exported base paths | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [harp from](harp_from.md) - Secret container generation commands | ||
|
Oops, something went wrong.