-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
152 additions
and
4 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
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,58 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present the Maru Authors | ||
|
||
// Package cmd contains the CLI commands for maru. | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/defenseunicorns/maru-runner/src/config" | ||
"github.com/defenseunicorns/maru-runner/src/config/lang" | ||
"github.com/spf13/cobra" | ||
"github.com/zalando/go-keyring" | ||
) | ||
|
||
var loginCmd = &cobra.Command{ | ||
Use: "login", | ||
PersistentPreRun: func(_ *cobra.Command, _ []string) { | ||
exitOnInterrupt() | ||
cliSetup() | ||
}, | ||
Short: lang.RootCmdShort, | ||
ValidArgsFunction: ListAutoCompleteTasks, | ||
Args: func(_ *cobra.Command, args []string) error { | ||
if len(args) > 1 { | ||
return fmt.Errorf("accepts 0 or 1 arg(s), received %d", len(args)) | ||
} | ||
return nil | ||
}, | ||
Run: func(_ *cobra.Command, args []string) { | ||
service := "my-app" | ||
user := "anon" | ||
password := "secret" | ||
|
||
// set password | ||
err := keyring.Set(service, user, password) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// get password | ||
secret, err := keyring.Get(service, user) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Println(secret) | ||
}, | ||
} | ||
|
||
func init() { | ||
initViper() | ||
rootCmd.AddCommand(loginCmd) | ||
runFlags := runCmd.Flags() | ||
runFlags.StringVarP(&config.TaskFileLocation, "file", "f", config.TasksYAML, lang.CmdRunFlag) | ||
runFlags.BoolVar(&dryRun, "dry-run", false, lang.CmdRunDryRun) | ||
} |
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,75 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present the Maru Authors | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// parseTokens parses the MY_CLI_TOKENS environment variable into a map. | ||
func parseTokens() (map[string]string, error) { | ||
tokensEnv := os.Getenv("MY_CLI_TOKENS") | ||
if tokensEnv == "" { | ||
return nil, fmt.Errorf("MY_CLI_TOKENS environment variable is not set") | ||
} | ||
|
||
// Initialize a map to store the tokens | ||
tokenMap := make(map[string]string) | ||
|
||
// Split the env var into key=value pairs separated by ";" | ||
pairs := strings.Split(tokensEnv, ";") | ||
for _, pair := range pairs { | ||
// Split each pair into "key=value" | ||
parts := strings.SplitN(pair, "=", 2) | ||
if len(parts) != 2 { | ||
return nil, fmt.Errorf("invalid token format: %s", pair) | ||
} | ||
|
||
key := strings.TrimSpace(parts[0]) // hostname:port | ||
value := strings.TrimSpace(parts[1]) // token | ||
tokenMap[key] = value | ||
} | ||
|
||
return tokenMap, nil | ||
} | ||
|
||
// getToken retrieves a token for a specific hostname and port. | ||
func getToken(hostname string, port int, tokenMap map[string]string) (string, error) { | ||
key := fmt.Sprintf("%s:%d", hostname, port) | ||
if token, exists := tokenMap[key]; exists { | ||
return token, nil | ||
} | ||
return "", fmt.Errorf("no token found for %s", key) | ||
} | ||
|
||
func main() { | ||
// Parse tokens from the environment variable | ||
tokenMap, err := parseTokens() | ||
if err != nil { | ||
fmt.Println("Error parsing tokens:", err) | ||
return | ||
} | ||
|
||
// Example usage | ||
hostname := "gitlab.enterprise.corp" | ||
port := 8080 | ||
|
||
token, err := getToken(hostname, port, tokenMap) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Printf("Token for %s:%d is %s\n", hostname, port, token) | ||
} | ||
|
||
// Try a non-existent port | ||
port = 9000 | ||
token, err = getToken(hostname, port, tokenMap) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
fmt.Printf("Token for %s:%d is %s\n", hostname, port, token) | ||
} | ||
} |