feat: updated docker auth secret creation
This commit is contained in:
parent
972646295a
commit
b2b43a98fd
21
main.go
21
main.go
|
|
@ -40,10 +40,16 @@ func main() {
|
|||
|
||||
var createDockerSecretCmd = &cobra.Command{
|
||||
Use: "create-docker-secret",
|
||||
Short: "Creates a Docker registry secret in Vault for GHCR authentication.",
|
||||
Short: "Creates a Docker registry secret in Vault for Docker Hub.",
|
||||
Run: runCreateDockerSecret,
|
||||
}
|
||||
|
||||
var createGhcrSecretCmd = &cobra.Command{
|
||||
Use: "create-ghcr-secret",
|
||||
Short: "Creates a Docker registry secret in Vault for GHCR.",
|
||||
Run: runCreateGhcrSecret,
|
||||
}
|
||||
|
||||
var createGithubSecretCmd = &cobra.Command{
|
||||
Use: "create-github-secret",
|
||||
Short: "Creates or updates a GitHub authentication secret in Vault.",
|
||||
|
|
@ -60,6 +66,7 @@ func main() {
|
|||
// Add commands to their parent commands
|
||||
repoCmd.AddCommand(initCmd)
|
||||
vaultCmd.AddCommand(createDockerSecretCmd)
|
||||
vaultCmd.AddCommand(createGhcrSecretCmd)
|
||||
vaultCmd.AddCommand(createGithubSecretCmd)
|
||||
rootCmd.AddCommand(repoCmd)
|
||||
rootCmd.AddCommand(vaultCmd)
|
||||
|
|
@ -96,11 +103,17 @@ func runInitRepo(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
func runCreateDockerSecret(cmd *cobra.Command, args []string) {
|
||||
if err := createAndStoreSecrets(); err != nil {
|
||||
fmt.Printf("[ERROR] Failed to create Docker registry secret: %v\n", err)
|
||||
if err := createDockerHubSecretWorkflow(); err != nil {
|
||||
fmt.Printf("[ERROR] Failed to create Docker Hub secret: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runCreateGhcrSecret(cmd *cobra.Command, args []string) {
|
||||
if err := createGhcrSecretWorkflow(); err != nil {
|
||||
fmt.Printf("[ERROR] Failed to create GHCR secret: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("[SUCCESS] Docker registry secret created successfully in Vault!")
|
||||
}
|
||||
|
||||
func runCreateGithubSecret(cmd *cobra.Command, args []string) {
|
||||
|
|
|
|||
87
vault.go
87
vault.go
|
|
@ -20,11 +20,13 @@ const (
|
|||
vaultRootTokenSecretName = "vault-unseal-keys"
|
||||
vaultRootTokenSecretKey = "vault-root"
|
||||
ghcrSecretPath = "secret/ghcr-auth"
|
||||
dockerHubSecretPath = "secret/dockerhub-auth"
|
||||
githubAuthSecretPath = "secret/github-auth"
|
||||
orgManagementSecretPath = "secret/org-management-pat"
|
||||
dockerConfigJSONKey = ".dockerconfigjson"
|
||||
orgManagementPATKey = "ORG_MANAGEMENT_PAT"
|
||||
ghcrRegistry = "ghcr.io"
|
||||
dockerHubRegistry = "docker.io"
|
||||
defaultVaultAddress = "https://127.0.0.1:8200"
|
||||
)
|
||||
|
||||
|
|
@ -161,33 +163,41 @@ func (vm *VaultManager) StoreSecret(path string, data map[string]string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// --- Main Workflow Function ---
|
||||
func createAndStoreSecrets() error {
|
||||
// 1. Get User Input
|
||||
username, registryToken, orgToken, err := getUserInput()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. Initialize Vault Manager
|
||||
// This handles kubectl checks, pod discovery, and token retrieval.
|
||||
// --- Main Workflow Functions ---
|
||||
func createDockerHubSecretWorkflow() error {
|
||||
vm, err := NewVaultManager()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize Vault manager: %w", err)
|
||||
}
|
||||
|
||||
// 3. Create and Store Docker Registry Secret
|
||||
dockerConfigJSON, err := createDockerConfig(username, registryToken)
|
||||
return createDockerHubSecret(vm)
|
||||
}
|
||||
|
||||
func createGhcrSecretWorkflow() error {
|
||||
vm, err := NewVaultManager()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize Vault manager: %w", err)
|
||||
}
|
||||
|
||||
return createGhcrSecrets(vm)
|
||||
}
|
||||
|
||||
func createGhcrSecrets(vm *VaultManager) error {
|
||||
fmt.Println("\n[INFO] Configuring secret for GitHub Container Registry (GHCR)...")
|
||||
username, registryToken, orgToken, err := getGHCRUserInput()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dockerConfigJSON, err := createDockerConfig(ghcrRegistry, username, registryToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Docker config: %w", err)
|
||||
}
|
||||
|
||||
dockerSecretData := map[string]string{dockerConfigJSONKey: dockerConfigJSON}
|
||||
if err := vm.StoreSecret(ghcrSecretPath, dockerSecretData); err != nil {
|
||||
return fmt.Errorf("failed to store Docker registry secret: %w", err)
|
||||
}
|
||||
|
||||
// 4. Create and Store Organization Management PAT
|
||||
orgSecretData := map[string]string{orgManagementPATKey: orgToken}
|
||||
if err := vm.StoreSecret(orgManagementSecretPath, orgSecretData); err != nil {
|
||||
return fmt.Errorf("failed to store organization management PAT: %w", err)
|
||||
|
|
@ -196,6 +206,26 @@ func createAndStoreSecrets() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func createDockerHubSecret(vm *VaultManager) error {
|
||||
fmt.Println("\n[INFO] Configuring secret for Docker Hub...")
|
||||
username, password, err := getUserInputForDockerHub()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dockerConfigJSON, err := createDockerConfig(dockerHubRegistry, username, password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Docker config: %w", err)
|
||||
}
|
||||
dockerSecretData := map[string]string{dockerConfigJSONKey: dockerConfigJSON}
|
||||
|
||||
if err := vm.StoreSecret(dockerHubSecretPath, dockerSecretData); err != nil {
|
||||
return fmt.Errorf("failed to store Docker Hub secret: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createAndStoreGithubAuthSecret() error {
|
||||
username, token, err := getUserInputForGithubAuth()
|
||||
if err != nil {
|
||||
|
|
@ -219,7 +249,7 @@ func createAndStoreGithubAuthSecret() error {
|
|||
}
|
||||
|
||||
// --- User Input and Helper Functions ---
|
||||
func getUserInput() (username, registryToken, orgToken string, err error) {
|
||||
func getGHCRUserInput() (username, registryToken, orgToken string, err error) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Enter your GitHub username: ")
|
||||
|
|
@ -246,6 +276,29 @@ func getUserInput() (username, registryToken, orgToken string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func getUserInputForDockerHub() (username, password string, err error) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Enter your Docker Hub username: ")
|
||||
username, err = reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
username = strings.TrimSpace(username)
|
||||
|
||||
fmt.Print("Enter your Docker Hub password or access token: ")
|
||||
passwordBytes, err := term.ReadPassword(int(syscall.Stdin))
|
||||
fmt.Println()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
password = string(passwordBytes)
|
||||
|
||||
if username == "" || password == "" {
|
||||
err = fmt.Errorf("username and password cannot be empty")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getUserInputForGithubAuth() (username, token string, err error) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
|
|
@ -304,11 +357,11 @@ func promptForToken(reader *bufio.Reader, prompt string) (string, error) {
|
|||
}
|
||||
|
||||
// createDockerConfig creates a Docker config JSON string.
|
||||
func createDockerConfig(username, token string) (string, error) {
|
||||
func createDockerConfig(registry, username, token string) (string, error) {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + token))
|
||||
config := DockerConfig{
|
||||
Auths: map[string]DockerAuth{
|
||||
ghcrRegistry: {
|
||||
registry: {
|
||||
Username: username,
|
||||
Password: token,
|
||||
Auth: auth,
|
||||
|
|
|
|||
Loading…
Reference in a new issue