github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/message/credentials.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package message provides a rich set of functions for displaying messages to the user.
     5  package message
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/Racer159/jackal/src/config"
    12  	"github.com/Racer159/jackal/src/types"
    13  	"github.com/pterm/pterm"
    14  )
    15  
    16  // Common constants for printing credentials
    17  const (
    18  	RegistryKey     = "registry"
    19  	RegistryReadKey = "registry-readonly"
    20  	GitKey          = "git"
    21  	GitReadKey      = "git-readonly"
    22  	ArtifactKey     = "artifact"
    23  	LoggingKey      = "logging"
    24  	AgentKey        = "agent"
    25  )
    26  
    27  // PrintCredentialTable displays credentials in a table
    28  func PrintCredentialTable(state *types.JackalState, componentsToDeploy []types.DeployedComponent) {
    29  	if len(componentsToDeploy) == 0 {
    30  		componentsToDeploy = []types.DeployedComponent{{Name: "logging"}, {Name: "git-server"}}
    31  	}
    32  
    33  	// Pause the logfile's output to avoid credentials being printed to the log file
    34  	if logFile != nil {
    35  		logFile.pause()
    36  		defer logFile.resume()
    37  	}
    38  
    39  	loginData := [][]string{}
    40  	if state.RegistryInfo.InternalRegistry {
    41  		loginData = append(loginData,
    42  			[]string{"Registry", state.RegistryInfo.PushUsername, state.RegistryInfo.PushPassword, "jackal connect registry", RegistryKey},
    43  			[]string{"Registry (read-only)", state.RegistryInfo.PullUsername, state.RegistryInfo.PullPassword, "jackal connect registry", RegistryReadKey},
    44  		)
    45  	}
    46  
    47  	for _, component := range componentsToDeploy {
    48  		// Show message if including logging stack
    49  		if component.Name == "logging" {
    50  			loginData = append(loginData, []string{"Logging", config.JackalLoggingUser, state.LoggingSecret, "jackal connect logging", LoggingKey})
    51  		}
    52  		// Show message if including git-server
    53  		if component.Name == "git-server" {
    54  			loginData = append(loginData,
    55  				[]string{"Git", state.GitServer.PushUsername, state.GitServer.PushPassword, "jackal connect git", GitKey},
    56  				[]string{"Git (read-only)", state.GitServer.PullUsername, state.GitServer.PullPassword, "jackal connect git", GitReadKey},
    57  				[]string{"Artifact Token", state.ArtifactServer.PushUsername, state.ArtifactServer.PushToken, "jackal connect git", ArtifactKey},
    58  			)
    59  		}
    60  	}
    61  
    62  	if len(loginData) > 0 {
    63  		header := []string{"Application", "Username", "Password", "Connect", "Get-Creds Key"}
    64  		Table(header, loginData)
    65  	}
    66  }
    67  
    68  // PrintComponentCredential displays credentials for a single component
    69  func PrintComponentCredential(state *types.JackalState, componentName string) {
    70  	switch strings.ToLower(componentName) {
    71  	case LoggingKey:
    72  		Notef("Logging credentials (username: %s):", config.JackalLoggingUser)
    73  		fmt.Println(state.LoggingSecret)
    74  	case GitKey:
    75  		Notef("Git Server push password (username: %s):", state.GitServer.PushUsername)
    76  		fmt.Println(state.GitServer.PushPassword)
    77  	case GitReadKey:
    78  		Notef("Git Server (read-only) password (username: %s):", state.GitServer.PullUsername)
    79  		fmt.Println(state.GitServer.PullPassword)
    80  	case ArtifactKey:
    81  		Notef("Artifact Server token (username: %s):", state.ArtifactServer.PushUsername)
    82  		fmt.Println(state.ArtifactServer.PushToken)
    83  	case RegistryKey:
    84  		Notef("Image Registry password (username: %s):", state.RegistryInfo.PushUsername)
    85  		fmt.Println(state.RegistryInfo.PushPassword)
    86  	case RegistryReadKey:
    87  		Notef("Image Registry (read-only) password (username: %s):", state.RegistryInfo.PullUsername)
    88  		fmt.Println(state.RegistryInfo.PullPassword)
    89  	default:
    90  		Warn("Unknown component: " + componentName)
    91  	}
    92  }
    93  
    94  // PrintCredentialUpdates displays credentials that will be updated
    95  func PrintCredentialUpdates(oldState *types.JackalState, newState *types.JackalState, services []string) {
    96  	// Pause the logfile's output to avoid credentials being printed to the log file
    97  	if logFile != nil {
    98  		logFile.pause()
    99  		defer logFile.resume()
   100  	}
   101  
   102  	for _, service := range services {
   103  
   104  		HorizontalRule()
   105  
   106  		switch service {
   107  		case RegistryKey:
   108  			oR := oldState.RegistryInfo
   109  			nR := newState.RegistryInfo
   110  			Title("Registry", "the information used to interact with Jackal's container image registry")
   111  			pterm.Println()
   112  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oR.Address, nR.Address, false))
   113  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oR.PushUsername, nR.PushUsername, false))
   114  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oR.PushPassword, nR.PushPassword, true))
   115  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oR.PullUsername, nR.PullUsername, false))
   116  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oR.PullPassword, nR.PullPassword, true))
   117  		case GitKey:
   118  			oG := oldState.GitServer
   119  			nG := newState.GitServer
   120  			Title("Git Server", "the information used to interact with Jackal's GitOps Git Server")
   121  			pterm.Println()
   122  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oG.Address, nG.Address, false))
   123  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oG.PushUsername, nG.PushUsername, false))
   124  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Password"), compareStrings(oG.PushPassword, nG.PushPassword, true))
   125  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Pull Username"), compareStrings(oG.PullUsername, nG.PullUsername, false))
   126  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Pull Password"), compareStrings(oG.PullPassword, nG.PullPassword, true))
   127  		case ArtifactKey:
   128  			oA := oldState.ArtifactServer
   129  			nA := newState.ArtifactServer
   130  			Title("Artifact Server", "the information used to interact with Jackal's Artifact Server")
   131  			pterm.Println()
   132  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("URL Address"), compareStrings(oA.Address, nA.Address, false))
   133  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Username"), compareStrings(oA.PushUsername, nA.PushUsername, false))
   134  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Push Token"), compareStrings(oA.PushToken, nA.PushToken, true))
   135  		case AgentKey:
   136  			oT := oldState.AgentTLS
   137  			nT := newState.AgentTLS
   138  			Title("Agent TLS", "the certificates used to connect to Jackal's Agent")
   139  			pterm.Println()
   140  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Certificate Authority"), compareStrings(string(oT.CA), string(nT.CA), true))
   141  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Public Certificate"), compareStrings(string(oT.Cert), string(nT.Cert), true))
   142  			pterm.Printfln("    %s: %s", pterm.Bold.Sprint("Private Key"), compareStrings(string(oT.Key), string(nT.Key), true))
   143  		}
   144  	}
   145  
   146  	pterm.Println()
   147  }
   148  
   149  func compareStrings(old string, new string, secret bool) string {
   150  	if new == old {
   151  		if secret {
   152  			return "**sanitized** (unchanged)"
   153  		}
   154  		return fmt.Sprintf("%s (unchanged)", old)
   155  	}
   156  	if secret {
   157  		return fmt.Sprintf("%s -> %s", pterm.FgRed.Sprint("**existing (sanitized)**"), pterm.FgGreen.Sprint("**replacement (sanitized)**"))
   158  	}
   159  	return fmt.Sprintf("%s -> %s", pterm.FgRed.Sprint(old), pterm.FgGreen.Sprint(new))
   160  }