github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/admin/initial_password.go (about)

     1  package admin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/client-go/kubernetes"
    10  	"k8s.io/client-go/tools/clientcmd"
    11  
    12  	"github.com/argoproj/argo-cd/v3/util/cli"
    13  	"github.com/argoproj/argo-cd/v3/util/errors"
    14  )
    15  
    16  const initialPasswordSecretName = "argocd-initial-admin-secret"
    17  
    18  // NewInitialPasswordCommand defines a new command to retrieve Argo CD initial password.
    19  func NewInitialPasswordCommand() *cobra.Command {
    20  	var clientConfig clientcmd.ClientConfig
    21  	command := cobra.Command{
    22  		Use:   "initial-password",
    23  		Short: "Prints initial password to log in to Argo CD for the first time",
    24  		Run: func(_ *cobra.Command, _ []string) {
    25  			config, err := clientConfig.ClientConfig()
    26  			errors.CheckError(err)
    27  			namespace, _, err := clientConfig.Namespace()
    28  			errors.CheckError(err)
    29  
    30  			kubeClientset := kubernetes.NewForConfigOrDie(config)
    31  			secret, err := kubeClientset.CoreV1().Secrets(namespace).Get(context.Background(), initialPasswordSecretName, metav1.GetOptions{})
    32  			errors.CheckError(err)
    33  
    34  			if initialPass, ok := secret.Data["password"]; ok {
    35  				fmt.Println(string(initialPass))
    36  				fmt.Println("\n This password must be only used for first time login. We strongly recommend you update the password using `argocd account update-password`.")
    37  			}
    38  		},
    39  	}
    40  	clientConfig = cli.AddKubectlFlagsToCmd(&command)
    41  
    42  	return &command
    43  }