Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» login command - print selected username to avoid ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
πŸ’₯Hedi Ghediri committed Jul 31, 2024
1 parent 015911a commit a496e21
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 36 deletions.
26 changes: 14 additions & 12 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"syscall"

"github.com/criteo/command-launcher/internal/command"
Expand Down Expand Up @@ -50,17 +52,17 @@ The credential will be stored in your system vault.`, appCtx.PasswordEnvVar()),
appCtx, _ := context.AppContext()
username := loginFlags.username
if username == "" {
username = os.Getenv(appCtx.UsernameEnvVar())
if username == "" {
fmt.Printf("Please enter your user name: ")
nb, err := fmt.Scan(&username)
if err != nil {
return err
}

if nb != 1 {
return fmt.Errorf("invalid entries (expected only one argument)")
}
reader := bufio.NewReader(os.Stdin)
defaultUser := defaultUsername()
fmt.Printf("Please enter your user name [%s]: ", defaultUser)
input, err := reader.ReadString('\n')
if err != nil {
return err
}
if input = strings.TrimSpace(input); input != "" {
username = input
} else {
username = defaultUser
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ The credential will be stored in your system vault.`, appCtx.PasswordEnvVar()),
return nil
},
}
loginCmd.Flags().StringVarP(&loginFlags.username, "user", "u", defaultUsername(), "User name")
loginCmd.Flags().StringVarP(&loginFlags.username, "user", "u", "", "User name")
loginCmd.Flags().StringVarP(&loginFlags.password, "password", "p", "", "User password")

rootCmd.AddCommand(loginCmd)
Expand Down
10 changes: 5 additions & 5 deletions test/integration/test-consent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $CL_PATH login -u test-user -p test-password

echo "> test consent disabled"
$CL_PATH config enable_user_consent false
RESULT=$($CL_PATH bonjour-consent)
RESULT=$($CL_PATH print-credentials-with-consent)
echo "$RESULT"
echo "$RESULT" | grep -q "test-user"
if [ $? -eq 0 ]; then
Expand All @@ -40,7 +40,7 @@ fi

echo "> test consent enabled - user refused"
$CL_PATH config enable_user_consent true
RESULT=$(echo 'n' | $CL_PATH bonjour-consent)
RESULT=$(echo 'n' | $CL_PATH print-credentials-with-consent)
echo "$RESULT"
echo "$RESULT" | grep -q "authorize the access?"
if [ $? -eq 0 ]; then
Expand Down Expand Up @@ -68,7 +68,7 @@ fi

echo "> test consent enabled - user authorized"
$CL_PATH config enable_user_consent true
RESULT=$(echo 'y' | $CL_PATH bonjour-consent)
RESULT=$(echo 'y' | $CL_PATH print-credentials-with-consent)
echo "$RESULT"
echo "$RESULT" | grep -q "authorize the access?"
if [ $? -eq 0 ]; then
Expand Down Expand Up @@ -96,7 +96,7 @@ fi

echo "> test consent authorized - should not request authorization again"
$CL_PATH config enable_user_consent true
RESULT=$(echo 'y' | $CL_PATH bonjour-consent)
RESULT=$(echo 'y' | $CL_PATH print-credentials-with-consent)
echo "$RESULT"
echo "$RESULT" | grep -q "authorize the access?"
if [ $? -eq 0 ]; then
Expand All @@ -109,7 +109,7 @@ fi
echo "> test consent authorized - should request authorization once expired"
sleep 5
$CL_PATH config enable_user_consent true
RESULT=$(echo 'y' | $CL_PATH bonjour-consent)
RESULT=$(echo 'y' | $CL_PATH print-credentials-with-consent)
echo "$RESULT"
echo "$RESULT" | grep -q "authorize the access?"
if [ $? -eq 0 ]; then
Expand Down
65 changes: 65 additions & 0 deletions test/integration/test-login.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# required environment varibale
# CL_PATH
# CL_HOME
# OUTPUT_DIR

SCRIPT_DIR=${1:-$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )}

# First copy the dropin packages for test
rm -rf $CL_HOME/dropins
mkdir -p $CL_HOME/dropins
cp -R $SCRIPT_DIR/../packages-src/login $CL_HOME/dropins


###
# login without -u
###
RESULT=$(echo | $CL_PATH login -p test-password) # The echo simulates an empty input from the user
echo -e "Actual:\n$RESULT"
EXPECTED_USERNAME_PROMPT="Please enter your user name [$(whoami)]: "
TEST_DESCRIPTION="should prompt '$EXPECTED_USERNAME_PROMPT'"
if ! grep -qF "$EXPECTED_USERNAME_PROMPT" <(echo "$RESULT"); then
echo "KO - $TEST_DESCRIPTION"
exit 1
else
echo "OK - $TEST_DESCRIPTION"
fi

RESULT=$($CL_PATH print-credentials)
echo -e "Actual:\n$RESULT"
EXPECTED_USERNAME="CL_USERNAME: $(whoami)"
TEST_DESCRIPTION="should have username $(whoami)"
if ! grep -qF "$EXPECTED_USERNAME" <(echo "$RESULT"); then
echo "KO - $TEST_DESCRIPTION"
exit 1
else
echo "OK - $TEST_DESCRIPTION"
fi


###
# login with -u
###
RESULT=$($CL_PATH login -u test-user -p test-password)
echo -e "Actual:\n$RESULT"
EXPECTED_USERNAME_PROMPT="Please enter your user name [test-user]: "
TEST_DESCRIPTION="should prompt '$EXPECTED_USERNAME_PROMPT'"
if ! grep -qF "$EXPECTED_USERNAME_PROMPT" <(echo "$RESULT"); then
echo "KO - $TEST_DESCRIPTION"
exit 1
else
echo "OK - $TEST_DESCRIPTION"
fi

RESULT=$($CL_PATH print-credentials)
echo -e "Actual:\n$RESULT"
EXPECTED_USERNAME="CL_USERNAME: test-user"
TEST_DESCRIPTION="should have username test-user"
if ! grep -qF "$EXPECTED_USERNAME" <(echo "$RESULT"); then
echo "KO - $TEST_DESCRIPTION"
exit 1
else
echo "OK - $TEST_DESCRIPTION"
fi
6 changes: 3 additions & 3 deletions test/integration/test-system-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fi
echo "> test login extension without setup system package"
echo "* should NOT use username returned from extension"
RESULT=$($CL_PATH login -u test-user -p test-password)
RESULT=$($CL_PATH bonjour-consent)
RESULT=$($CL_PATH print-credentials-with-consent)
echo "$RESULT" | grep -q "SECRET_1"
if [ $? -ne 0 ]; then
# ok
Expand Down Expand Up @@ -105,7 +105,7 @@ echo "> test login extension enabled by system package config"
# set system package name
RESULT=$($CL_PATH config system_package system-pkg-demo)
$CL_PATH login -u test-user -p test-password
RESULT=$($CL_PATH bonjour-consent)
RESULT=$($CL_PATH print-credentials-with-consent)

echo "* should use username returned from extension"
echo "$RESULT" | grep -q "SECRET_1"
Expand Down Expand Up @@ -157,7 +157,7 @@ else
exit 1
fi

echo "$RESULT" | grep 'dropin bonjour default bonjour-consent'
echo "$RESULT" | grep 'dropin print-credentials default print-credentials-with-consent'
if [ $? -eq 0 ]; then
echo "OK"
else
Expand Down
6 changes: 0 additions & 6 deletions test/packages-src/login/bonjour.bat

This file was deleted.

6 changes: 0 additions & 6 deletions test/packages-src/login/bonjour.sh

This file was deleted.

20 changes: 16 additions & 4 deletions test/packages-src/login/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
{
"pkgName": "bonjour",
"pkgName": "print-credentials",
"version": "1.0.0",
"cmds": [
{
"name": "bonjour-consent",
"name": "print-credentials-with-consent",
"type": "executable",
"short": "print bonjour from command launcher",
"executable": "{{.PackageDir}}/bonjour.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}",
"short": "print credentials from command launcher",
"executable": "{{.PackageDir}}/print-credentials.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}",
"args": [],
"requiredFlags": [
"name\t n\t greeting name",
"language\t l\tgreeting language"
],
"checkFlags": true,
"requestedResources": [ "USERNAME", "PASSWORD", "AUTH_TOKEN" ]
},
{
"name": "print-credentials",
"type": "executable",
"short": "print credentials from command launcher",
"executable": "{{.PackageDir}}/print-credentials.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}",
"args": [],
"requiredFlags": [
"name\t n\t greeting name",
"language\t l\tgreeting language"
],
"checkFlags": true
}
]
}
5 changes: 5 additions & 0 deletions test/packages-src/login/print-credentials.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@ECHO off

ECHO CL_USERNAME: %CL_USERNAME%
ECHO CL_PASSWORD: %CL_PASSWORD%
ECHO CL_AUTH_TOKEN: %CL_AUTH_TOKEN%
5 changes: 5 additions & 0 deletions test/packages-src/login/print-credentials.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

echo "CL_USERNAME: $CL_USERNAME"
echo "CL_PASSWORD: $CL_PASSWORD"
echo "CL_AUTH_TOKEN: $CL_AUTH_TOKEN"

0 comments on commit a496e21

Please sign in to comment.