Skip to content

Commit

Permalink
rewrite half of application. Updated readme, added some more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ybelMekk committed Sep 10, 2021
1 parent 5e2919d commit 34063a9
Show file tree
Hide file tree
Showing 19 changed files with 538 additions and 286 deletions.
83 changes: 62 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Available commands:

- apply
- version
- get

For help on individual commands, add `--help` following the command name.
For help on individual commands, add `--help` short: `-h`.

### Flags

Expand All @@ -67,43 +68,56 @@ Flags provide modifiers to control how the action command operates.

* `--team`
* short `-t`: teamnamespace (default not supported).
* get
* `--secret-name`
* short `-s`: Secretname for your aiven application.

### Optional

* apply
* `--pool`
* short `-p` default `nav-dev`: Preferred kafka pool.
* short `-p` default: `nav-dev`: Preferred kafka pool.

* `--expire`
* short `-e` default `1`: Time in days the created secret should be valid.
* short `-e` default: `1`: Time in days the created secret should be valid.

* `--dest`
* short `-d` default `current`: Path to directory where secrets will be dropped of. For `current` with an
additional folder, e.g: `/.config`
* short `-d` default: `current`: Path to directory where secrets will be dropped of. For `current` with
subfolder folder, e.g: `/.config`

* `--secret-name`
* short `-s` default `namespace-username-(random-id)`: Preferred secret-name instead of the generated.
* short `-s` default: `namespace-username-(random-id)`: Preferred secret-name instead of the generated.

* version
* `--commit`
* short `-i` default `false` : Get detailed information about this debuk version
* short `-i` default: `false` : Get detailed information about this debuk version

* get
* `--dest`
* short `-d` default: `current`: Path to directory where secrets will be dropped of. For `current` with
subfolder folder, e.g: `/.config`
* `--config`
* short `-c`: default: `all`: Config type, `all || kcat || .env`. `all` generates both .env and kcat config
files.

## Available configuration files

## Available files
After Successful `debuk` command a set of files will be available in `current` folder.

After successful `debuk` command a set of files will be available in `current` folder.
### All

- client-keystore.p12
- client-truststore.jks
- `username`.yaml (the yaml applied to specified namespace)
- kafka-ca.cert
- kafka-certificate.crt
- kafka-private-key.pem
- kafka-schema-registry.env
- kafka-secret.env
- kcat.conf

### Examples files
#### Example

#### `username`.yaml
##### `username`.yaml

```yaml
apiVersion: aiven.nais.io/v1
Expand All @@ -120,7 +134,42 @@ spec:
timeToLive: 1
```
#### kcat.conf
### .env
- client-keystore.p12
- client-truststore.jks
- kafka-ca.cert
- kafka-certificate.crt
- kafka-private-key.pem
- kafka-secret.env
#### kafka-secret.env
```Properties
KAFKA_BROKERS:brokerurl.aivencloud.com:26484
KAFKA_PRIVATE_KEY=/path/to/kafka-private-key.pem
client.keystore.p12=/path/to/client-keystore.p12
client.truststore.jks=/path/to/.envs/client-truststore.jks
KAFKA_CA=/path/to/.envs/kafka-ca.cert
KAFKA_CERTIFICATE=/path/to/.envs/kafka-certificate.crt
KAFKA_CREDSTORE_PASSWORD:password
KAFKA_SCHEMA_REGISTRY:https://registry-url.aivencloud.com:26487
KAFKA_SCHEMA_REGISTRY_PASSWORD:password
KAFKA_SCHEMA_REGISTRY_USER:my-user
```

#### Example

### kcat

- kafka-ca.cert
- kafka-certificate.crt
- kafka-private-key.pem
- kcat.conf

#### Example

##### kcat.conf

```Properties
# Debuked 2021-09-01 15:26:00
Expand Down Expand Up @@ -155,14 +204,6 @@ kcat \

For more details [aiven-kcat-help](https://help.aiven.io/en/articles/2607674-using-kafkacat)

#### kafka-schema-registry.env

```Properties
KAFKA_SCHEMA_REGISTRY_USER:my-team.my-user-cq3bvnum
KAFKA_SCHEMA_REGISTRY:https://bootstrap-server.aivencloud.com:26487
KAFKA_SCHEMA_REGISTRY_PASSWORD:password
```

## Flow

![Debuk under the hood](doc/debuk.png)
Expand Down
43 changes: 10 additions & 33 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package cmd

import (
"fmt"
"github.com/nais/debuk/pkg/aiven/generate"
"github.com/nais/debuk/cmd/helpers"
"github.com/nais/debuk/pkg/generate"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
KafkaNavDev = "nav-dev"
KafkaNavProd = "nav-prod"
KafkaNavDev = "nav-dev"
KafkaNavProd = "nav-prod"
KafkaNavIntegrationTest = "nav-integration-test"
)

Expand All @@ -19,33 +19,28 @@ var applyCommand = &cobra.Command{
Long: `Will apply a aivenApplication based on information given and extract credentials`,
RunE: func(cmd *cobra.Command, args []string) error {

username, err := getString(cmd, UsernameFlag, true)
username, err := helpers.GetString(cmd, UsernameFlag, true)
if err != nil {
return err
}

team, err := getString(cmd, TeamFlag, true)
team, err := helpers.GetString(cmd, TeamFlag, true)
if err != nil {
return err
}

pool, _ := getString(cmd, PoolFlag, false)
if pool != KafkaNavDev && pool != KafkaNavProd && pool != KafkaNavIntegrationTest {
pool, _ := helpers.GetString(cmd, PoolFlag, false)
if pool != KafkaNavDev && pool != KafkaNavProd && pool != KafkaNavIntegrationTest {
return fmt.Errorf("valid values for '--%s': %s | %s | %s", PoolFlag, KafkaNavDev, KafkaNavProd, KafkaNavIntegrationTest)
}

dest, err := getString(cmd, DestFlag, false)
dest, err := helpers.GetString(cmd, DestFlag, false)
if err != nil {
return fmt.Errorf("getting %s: %s", DestFlag, err)
}

dest, err = DefaultDestination(dest)
if err != nil {
return fmt.Errorf("setting destination: %s", err)
}

expiry, err := cmd.Flags().GetInt(ExpireFlag)
secretName, err := getString(cmd, SecretNameFlag, false)
secretName, err := helpers.GetString(cmd, SecretNameFlag, false)
if err != nil {
return fmt.Errorf("getting flag %s", err)
}
Expand All @@ -56,21 +51,3 @@ var applyCommand = &cobra.Command{
return nil
},
}

func getString(cmd *cobra.Command, flag string, required bool) (string, error) {
env := viper.GetString(flag)
if env != "" {
return env, nil
}
arg, err := cmd.Flags().GetString(flag)
if err != nil {
return "", fmt.Errorf("getting %s: %s", flag, err)
}
if arg == "" {
if required {
return "", fmt.Errorf("%s is reqired", flag)
}
}
return arg, nil

}
46 changes: 46 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
"github.com/nais/debuk/cmd/helpers"
"github.com/nais/debuk/pkg/consts"
"github.com/nais/debuk/pkg/generate"
"github.com/spf13/cobra"
"os"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "The specified config format will get the secret and generated to 'current' location",
Run: func(cmd *cobra.Command, args []string) {
secretName, err := helpers.GetString(cmd, SecretNameFlag, true)
if err != nil {
fmt.Printf("getting %s: %s", SecretNameFlag, err)
os.Exit(1)
}

configType, err := helpers.GetString(cmd, ConfigFlag, false)
if err != nil {
fmt.Printf("getting %s: %s", ConfigFlag, err)
os.Exit(1)
}

if configType != consts.ENV && configType != consts.ALL && configType != consts.KCAT {
fmt.Printf("valid args: %s | %s | %s", consts.ENV, consts.KCAT, consts.ALL)
os.Exit(1)
}

dest, err := helpers.GetString(cmd, DestFlag, false)
if err != nil {
fmt.Printf("getting %s: %s", DestFlag, err)
os.Exit(1)
}

dest, err = helpers.DefaultDestination(dest)
if err != nil {
fmt.Printf("an error %s", err)
os.Exit(1)
}
generate.TypeConfig(configType, dest, secretName)
},
}
29 changes: 28 additions & 1 deletion cmd/config.go → cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd
package helpers

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"strings"
)
Expand Down Expand Up @@ -31,5 +33,30 @@ func DefaultDestination(dest string) (string, error) {
}
}
return newPath, nil
}

func GetString(cmd *cobra.Command, flag string, required bool) (string, error) {
env := viper.GetString(flag)
if env != "" {
return env, nil
}
arg, err := cmd.Flags().GetString(flag)
if err != nil {
return "", fmt.Errorf("getting %s: %s", flag, err)
}
if arg == "" {
if required {
return "", fmt.Errorf("%s is reqired", flag)
}
}
return arg, nil
}

func Contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
39 changes: 29 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

var (
VERSION string
COMMIT string
DATE string
VERSION string
COMMIT string
DATE string
BUILT_BY string

rootCmd = &cobra.Command{
Expand All @@ -38,16 +38,28 @@ const (
TeamFlag = "team"
UsernameFlag = "username"
DestFlag = "dest"
ConfigFlag = "config"
ExpireFlag = "expire"
PoolFlag = "pool"
SecretNameFlag = "secret-name"

CommitInformation = "commit"
CommitInformation = "commit"
)

func init() {
cobra.OnInitialize(initConfig)
initApplyCmd()
initVersionCmd()
initGetCmd()
}

func initConfig() {
viper.SetEnvPrefix("DEBUK")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
}

func initApplyCmd() {
applyCommand.Flags().StringP(UsernameFlag, "u", "", "Username for the aivenApplication configuration (required)")
viper.BindPFlag(UsernameFlag, applyCommand.Flags().Lookup(UsernameFlag))

Expand All @@ -65,16 +77,23 @@ func init() {

applyCommand.Flags().StringP(SecretNameFlag, "s", "", "Preferred secret-name instead of generated (optional)")
viper.BindPFlag(SecretNameFlag, applyCommand.Flags().Lookup(SecretNameFlag))
rootCmd.AddCommand(applyCommand)
}

func initVersionCmd() {
versionCmd.Flags().BoolP(CommitInformation, "i", false, "Detailed commit information for this debuk version (optional)")
viper.BindPFlag(CommitInformation, versionCmd.Flags().Lookup(DestFlag))

rootCmd.AddCommand(applyCommand)
rootCmd.AddCommand(versionCmd)
}

func initConfig() {
viper.SetEnvPrefix("DEBUK")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
func initGetCmd() {
getCmd.Flags().StringP(DestFlag, "d", "", "Path to directory where secrets will be dropped of. For current './creds' (optional)")
viper.BindPFlag(DestFlag, getCmd.Flags().Lookup(DestFlag))

getCmd.Flags().StringP(ConfigFlag, "c", "all", "Type of config do be generated, supported ( .env || kcat || all ) (optional)")
viper.BindPFlag(ConfigFlag, getCmd.Flags().Lookup(ConfigFlag))

getCmd.Flags().StringP(SecretNameFlag, "s", "", "Secretname specified for aiven application (required)")
viper.BindPFlag(SecretNameFlag, getCmd.Flags().Lookup(SecretNameFlag))
rootCmd.AddCommand(getCmd)
}
Loading

0 comments on commit 34063a9

Please sign in to comment.