From f0fb57c3ebfd43b39c2fdb9feae68594e733c22e Mon Sep 17 00:00:00 2001 From: Rilder Almeida Date: Thu, 15 Sep 2022 11:49:37 -0300 Subject: [PATCH 1/4] =?UTF-8?q?[WIP]=20Inicia=20a=20implementa=C3=A7=C3=A3?= =?UTF-8?q?o=20de=20multicontexts=20outras=20features:=20melhoria=20dos=20?= =?UTF-8?q?setup=20e=20das=20vari=C3=A1veis=20defaults?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- credentials | 3 ++ kcompose.sh | 107 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 credentials diff --git a/credentials b/credentials new file mode 100644 index 0000000..76e8f82 --- /dev/null +++ b/credentials @@ -0,0 +1,3 @@ +sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="1" password="1"; +security.protocol=SASL_PLAINTEXT +sasl.mechanism=PLAIN diff --git a/kcompose.sh b/kcompose.sh index 5e17853..bacdd10 100755 --- a/kcompose.sh +++ b/kcompose.sh @@ -15,14 +15,21 @@ # along with this program. If not, see . set -f # Disables Globbing -defaultConfigFile=$HOME/.kcompose/config +configPath=$HOME/.kcompose/ + +defaultConfigPath=${configPath}default/ +defaultConfigFile=${defaultConfigPath}config + +defaultCredentialsPath=$HOME/.kcompose/default/ +defaultCredentialsFile=${defaultCredentialsPath}credentials # Config chain # Defaults broker="localhost:9092" -credentialsFile="" kafkaLocation="/usr/share/kcompose/kafka" +credentialsFile="" configFile=${KCOMPOSE_CONFIG_FILE:-$defaultConfigFile} +contextCurrent="default" # Config file readConfig() { @@ -119,7 +126,7 @@ Authentication type must be one of: credentialsFile="" ;; "SASL/SCRAM256") - ask credentialsFile "Credentials File" $HOME/.kcompose/credentials + ask credentialsFile "Credentials File" $defaultCredentialsFile mkdir -p $(dirname $credentialsFile) ask username "Username" "" ask password "Password" "" @@ -135,7 +142,7 @@ EOF ;; "SASL/PLAIN") - ask credentialsFile "Credentials File" $HOME/.kcompose/credentials + ask credentialsFile "Credentials File" $defaultCredentialsFile mkdir -p $(dirname $credentialsFile) ask username "Username" "" ask password "Password" "" @@ -164,7 +171,22 @@ EOF } setup() { - ask configFile "Configuration file" $configFile + # TODO: helptext + usage + + # TODO: separate configPath from configFile + if [ -z "$1" ]; then + ask configFile "Configuration file" $configFile + fi + if [ -f $configFile ]; then + ask overwrite "Overwrite config file?" "n" + if [ "$overwrite" != "y" ]; then + exit + fi + fi + + configFile=$1 + validate_filename $configFile + ask broker "kafka brokers" $broker ask kafkaLocation "Kafka Location" $kafkaLocation @@ -177,6 +199,81 @@ setup() { fi } +# context is a function that manages the context of the config and credentials files +context() { + helpText="Usage: $programName context [new, set, import, list, delete] [contextName]\n" + helpText+="\tnew\t\tCreate a new context\n" + helpText+="\tset\t\tSet the current context\n" + helpText+="\tlist\t\tList all contexts\n" + helpText+="\tdelete\t\tDelete a context\n" + helpText+="\tcontextName\tName of the context\n" + + checkNArgs $1 + case $1 in + "new") + checkNArgs $2 + context_new $2 + ;; + *) + usage + ;; + esac +} + +# context_new creates a new context +context_new() { + checkNArgs $1 + contextName=$1 + validate_filename $contextName + + # TODO: create a new context + + configFile=$HOME/.kcompose/$contextName/config + credentialsFile=$HOME/.kcompose/$contextName/credentials +} + +# validate_filename checks if the filename is valid +validate_filename() { + __filename=$1 + val=$(echo "${#__filename}") + + if [[ $__filename == "default" ]]; then + echo "Context name cannot be 'default'" + exit + fi + + if [[ $__filename == "" ]]; then + echo "Filename: $__filename cannot be empty" + exit + fi + + if [[ $__filename == "." ]] || [[ $__filename == ".." ]]; then + # "." and ".." are added automatically and always exist, so you can't have a + # file named . or .. // https://askubuntu.com/a/416508/660555 + echo "Filename: $__filename is not valid" + exit + fi + + if [ $val -gt 255 ]; then + # String's length check + echo "Filename: $__filename has more than 255 characters" + exit + fi + + if ! [[ $__filename =~ ^[0-9a-zA-Z._-]+$ ]]; then + # Checks whether valid characters exist + echo "Filename: $__filename has invalid characters" + exit + fi + + ___filename=$(echo $__filename | cut -c1-1) + if ! [[ $___filename =~ ^[0-9a-zA-Z.]+$ ]]; then + # Checks the first character + echo "Filename: $__filename has invalid first character" + exit + fi +} + # commands helpText="Usage: $programName [topic, acl, produce, consume, group, env, config, setup, login, user]\n" helpText+="\ttopic\t\tTopic management\n" From 62ae279ef3fe52b8cc955bfb64c3f92c8be19b8c Mon Sep 17 00:00:00 2001 From: Rilder Almeida Date: Tue, 20 Sep 2022 09:59:56 -0300 Subject: [PATCH 2/4] =?UTF-8?q?finalizada=20implementa=C3=A7=C3=A3o=20da?= =?UTF-8?q?=20funcionalidade=20new=20do=20menu=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dasda | 3 +++ kcompose.sh | 65 ++++++++++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 36 deletions(-) create mode 100644 dasda diff --git a/dasda b/dasda new file mode 100644 index 0000000..f4364a2 --- /dev/null +++ b/dasda @@ -0,0 +1,3 @@ +sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="" password=""; +security.protocol=SASL_PLAINTEXT +sasl.mechanism=PLAIN diff --git a/kcompose.sh b/kcompose.sh index bacdd10..0eb2164 100755 --- a/kcompose.sh +++ b/kcompose.sh @@ -20,7 +20,7 @@ configPath=$HOME/.kcompose/ defaultConfigPath=${configPath}default/ defaultConfigFile=${defaultConfigPath}config -defaultCredentialsPath=$HOME/.kcompose/default/ +defaultCredentialsPath=${configPath}default/ defaultCredentialsFile=${defaultCredentialsPath}credentials # Config chain @@ -126,7 +126,7 @@ Authentication type must be one of: credentialsFile="" ;; "SASL/SCRAM256") - ask credentialsFile "Credentials File" $defaultCredentialsFile + credentialsFile=${configPath}$config/credentials mkdir -p $(dirname $credentialsFile) ask username "Username" "" ask password "Password" "" @@ -142,7 +142,8 @@ EOF ;; "SASL/PLAIN") - ask credentialsFile "Credentials File" $defaultCredentialsFile + # ask credentialsFile "Credentials File" $defaultCredentialsFile + credentialsFile=${configPath}$config/credentials mkdir -p $(dirname $credentialsFile) ask username "Username" "" ask password "Password" "" @@ -171,26 +172,25 @@ EOF } setup() { - # TODO: helptext + usage - - # TODO: separate configPath from configFile if [ -z "$1" ]; then - ask configFile "Configuration file" $configFile + ask config "Configuration name" "default" + else + config=$1 + validate_name $config fi + configFile=${configPath}$config/config + if [ -f $configFile ]; then - ask overwrite "Overwrite config file?" "n" + ask overwrite "Configuration \"$config\" already exist, do you want overwrite it? [y/N]" "n" if [ "$overwrite" != "y" ]; then exit fi fi - - configFile=$1 - validate_filename $configFile - ask broker "kafka brokers" $broker + ask broker "Kafka brokers" $broker ask kafkaLocation "Kafka Location" $kafkaLocation - ask login "login now? (y/n)" "y" + ask login "Login now? (y/n)" "y" if [ "$login" = "y" ]; then login @@ -222,54 +222,47 @@ context() { # context_new creates a new context context_new() { - checkNArgs $1 - contextName=$1 - validate_filename $contextName - - # TODO: create a new context - - configFile=$HOME/.kcompose/$contextName/config - credentialsFile=$HOME/.kcompose/$contextName/credentials + setup $1 } -# validate_filename checks if the filename is valid -validate_filename() { - __filename=$1 - val=$(echo "${#__filename}") +# validate_name checks if the filename is valid +validate_name() { + __name=$1 + val=$(echo "${#__name}") - if [[ $__filename == "default" ]]; then + if [[ $__name == "default" ]]; then echo "Context name cannot be 'default'" exit fi - if [[ $__filename == "" ]]; then - echo "Filename: $__filename cannot be empty" + if [[ $__name == "" ]]; then + echo "Filename: $__name cannot be empty" exit fi - if [[ $__filename == "." ]] || [[ $__filename == ".." ]]; then + if [[ $__name == "." ]] || [[ $__name == ".." ]]; then # "." and ".." are added automatically and always exist, so you can't have a # file named . or .. // https://askubuntu.com/a/416508/660555 - echo "Filename: $__filename is not valid" + echo "Filename: $__name is not valid" exit fi if [ $val -gt 255 ]; then # String's length check - echo "Filename: $__filename has more than 255 characters" + echo "Filename: $__name has more than 255 characters" exit fi - if ! [[ $__filename =~ ^[0-9a-zA-Z._-]+$ ]]; then + if ! [[ $__name =~ ^[0-9a-zA-Z._-]+$ ]]; then # Checks whether valid characters exist - echo "Filename: $__filename has invalid characters" + echo "Filename: $__name has invalid characters" exit fi - ___filename=$(echo $__filename | cut -c1-1) - if ! [[ $___filename =~ ^[0-9a-zA-Z.]+$ ]]; then + ___name=$(echo $__name | cut -c1-1) + if ! [[ $___name =~ ^[0-9a-zA-Z.]+$ ]]; then # Checks the first character - echo "Filename: $__filename has invalid first character" + echo "Filename: $__name has invalid first character" exit fi } From 90073c08a477cb87cf5f26ac3098a0fb59115db6 Mon Sep 17 00:00:00 2001 From: Rilder Almeida Date: Thu, 22 Sep 2022 12:05:12 -0300 Subject: [PATCH 3/4] [feature] Cria funcionalidade context para trabalhar com multiplos contextos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problema: O kcompose não permitia nativamente trabalhar com multiplos context, tendo que setar a variável KCOMPOSE_CONFIG_FILE manualmente. Solução: Criar a funionalidade context, com as opções set, new, list, delete e show P.s.: As funções setup e login tiveram de ser alteradas, assim como algumas variáveis. --- kcompose.sh | 154 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 136 insertions(+), 18 deletions(-) diff --git a/kcompose.sh b/kcompose.sh index 0eb2164..df89fbc 100755 --- a/kcompose.sh +++ b/kcompose.sh @@ -23,13 +23,27 @@ defaultConfigFile=${defaultConfigPath}config defaultCredentialsPath=${configPath}default/ defaultCredentialsFile=${defaultCredentialsPath}credentials +if [ ! -f ${configPath}context ]; then + touch ${configPath}context +fi + +source ${configPath}context +currentContext=${KCOMPOSE_CONTEXT:-"default"} + +if [ -z "$KCOMPOSE_CONFIG_FILE" ]; then + if [ ! -d "${configPath}${currentContext}" ]; then + echo -e "WARNING: Context ${currentContext} not found\n\n" + echo -e "Please, set a valid context using the command:" + echo -e "kcompose context set \n" + fi +fi + # Config chain # Defaults broker="localhost:9092" kafkaLocation="/usr/share/kcompose/kafka" credentialsFile="" configFile=${KCOMPOSE_CONFIG_FILE:-$defaultConfigFile} -contextCurrent="default" # Config file readConfig() { @@ -95,6 +109,12 @@ ask() { eval "$result=\"$answer\"" } +saveContext() { + if [ -f ${configPath}context ]; then + echo "KCOMPOSE_CONTEXT=$1" > ${configPath}context + fi +} + saveConfigs() { mkdir -p $(dirname $configFile) cat >$configFile < Date: Thu, 22 Sep 2022 12:20:30 -0300 Subject: [PATCH 4/4] fix: remove trash files --- credentials | 3 --- dasda | 3 --- 2 files changed, 6 deletions(-) delete mode 100644 credentials delete mode 100644 dasda diff --git a/credentials b/credentials deleted file mode 100644 index 76e8f82..0000000 --- a/credentials +++ /dev/null @@ -1,3 +0,0 @@ -sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="1" password="1"; -security.protocol=SASL_PLAINTEXT -sasl.mechanism=PLAIN diff --git a/dasda b/dasda deleted file mode 100644 index f4364a2..0000000 --- a/dasda +++ /dev/null @@ -1,3 +0,0 @@ -sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="" password=""; -security.protocol=SASL_PLAINTEXT -sasl.mechanism=PLAIN