-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2151dc0
commit 468872c
Showing
5 changed files
with
131 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,17 @@ Copyright © 2023 Patrick Hermann [email protected] | |
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
ipservice "github.com/stuttgart-things/clusterbook/ipservice" | ||
"github.com/stuttgart-things/machineshop/internal" | ||
sthingsBase "github.com/stuttgart-things/sthingsBase" | ||
sthingsCli "github.com/stuttgart-things/sthingsCli" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
@@ -25,34 +29,74 @@ var getCmd = &cobra.Command{ | |
|
||
// FLAGS | ||
authMethod, _ := cmd.LocalFlags().GetString("auth") | ||
secretPath, _ := cmd.LocalFlags().GetString("path") | ||
outputFormat, _ := cmd.LocalFlags().GetString("output") | ||
path, _ := cmd.LocalFlags().GetString("path") | ||
output, _ := cmd.LocalFlags().GetString("output") | ||
destinationPath, _ := cmd.LocalFlags().GetString("destination") | ||
b64DecodeOption, _ := cmd.LocalFlags().GetBool("b64") | ||
secretSystem, _ := cmd.LocalFlags().GetString("system") | ||
system, _ := cmd.LocalFlags().GetString("system") | ||
|
||
// START LOGGING | ||
log.Info("AUTH-METHOD: ", authMethod) | ||
log.Info("SECRET-PATH: ", secretPath) | ||
log.Info("SECRET-SYSTEM: ", secretSystem) | ||
log.Info("PATH: ", path) | ||
log.Info("SYSTEM: ", system) | ||
|
||
switch system { | ||
|
||
case "ips": | ||
|
||
log.Info("⚡️ CONNECTING TO CLUSTERBOOK ⚡️") | ||
log.Info("CLUSTERBOOK SERVER: ", destinationPath) | ||
log.Info("COUNT IPS: ", sthingsBase.ConvertStringToInteger(output)) | ||
log.Info("NETWORK-KEY: ", path) | ||
|
||
clusterBookServer := destinationPath //"clusterbook.rke2.sthings-vsphere.labul.sva.de:443" | ||
secureConnection := "true" | ||
countIps := int32(sthingsBase.ConvertStringToInteger(output)) | ||
networkKey := path | ||
|
||
// SELECT CREDENTIALS BASED ON SECURECONNECTION | ||
conn, err := grpc.NewClient(clusterBookServer, internal.GetCredentials(secureConnection)) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
c := ipservice.NewIpServiceClient(conn) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
// Testen der GetIpAddressRange-Methode | ||
ipReq := &ipservice.IpRequest{ | ||
CountIpAddresses: countIps, | ||
NetworkKey: networkKey, | ||
} | ||
|
||
ipRes, err := c.GetIpAddressRange(ctx, ipReq) | ||
if err != nil { | ||
log.Fatalf("could not get IP address range: %v", err) | ||
} | ||
|
||
log.Printf("Available IPs: %s", ipRes.IpAddressRange) | ||
|
||
switch secretSystem { | ||
case "vault": | ||
|
||
log.Info("AUTH-METHOD: ", authMethod) | ||
|
||
// CHECK FOR VAULT ENV VARS | ||
vaultAuthType, vaultAuthFound := sthingsCli.VerifyVaultEnvVars() | ||
log.Info("⚡️ VAULT CREDENDITALS ⚡️", vaultAuthType) | ||
|
||
internal.VerifyVaultAuthType(vaultAuthType, log, vaultAuthFound) | ||
|
||
// GET SECRET VALUE | ||
secretValue := sthingsCli.GetVaultSecretValue(secretPath, os.Getenv("VAULT_TOKEN")) | ||
internal.HandleRenderOutput(outputFormat, destinationPath, secretValue, b64DecodeOption, true) | ||
secretValue := sthingsCli.GetVaultSecretValue(path, os.Getenv("VAULT_TOKEN")) | ||
internal.HandleRenderOutput(output, destinationPath, secretValue, b64DecodeOption, true) | ||
|
||
case "sops": | ||
fmt.Println("SOPS") | ||
|
||
// GET SECRET PARAMETERS | ||
secretParameters := strings.Split(secretPath, ":") | ||
secretParameters := strings.Split(path, ":") | ||
secretKeyFile := secretParameters[0] | ||
secretKey := secretParameters[1] | ||
log.Info("SECRET KEY: ", secretKey) | ||
|
@@ -100,7 +144,7 @@ func init() { | |
getCmd.Flags().String("auth", "approle", "vault auth method") | ||
getCmd.Flags().String("path", "", "path to vault secret") | ||
getCmd.Flags().String("system", "vault", "secret system: vault|sops") | ||
getCmd.Flags().String("output", "stdout", "outputFormat stdout|file") | ||
getCmd.Flags().String("output", "stdout", "output stdout|file") | ||
getCmd.Flags().String("destination", "", "path to output (if output file)") | ||
getCmd.Flags().Bool("b64", false, "decode base64 for output") | ||
} |
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
Oops, something went wrong.