[IMPROVED] Crear script para encriptar token de Cortana con SOPS

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mauro Rosero P. 2025-03-11 07:53:23 -05:00
parent e9211614c3
commit c62a89223d
Signed by: mrosero
GPG key ID: 83BD2A5F674B7E26

158
bin/cortana_token.sh Executable file
View file

@ -0,0 +1,158 @@
#!/bin/bash
#
# cortana_token.sh
# Modified: 2025/03/11
# Derechos de Autor (C) [2025] [Mauro Rosero P. <mauro@rosero.one> (mauro.rosero@gmail.com)]
#
# Este programa es software libre: usted puede redistribuirlo y/o modificarlo
# bajo los términos de la Licencia Pública Affero General de GNU tal como
# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia,
# o (a su elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil,
# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la
# Licencia Pública Affero General de GNU para obtener más detalles.
#
# Debería haber recibido una copia de la Licencia Pública Affero General
# junto con este programa. Si no la recibió, consulte <https://www.gnu.org/licenses/>.
# Script para encriptar el token de Cortana (Claude Code) usando SOPS
DEVSPATH=devs
BIN_HOME=$HOME/$DEVSPATH
BIN_MESG=bin/msg
BIN_LIBS=bin/lib
BIN_CFGS=bin/config
VERSION=1.0.0
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# LOAD BASE BASH LIBRARY
source $BIN_HOME/$BIN_LIBS/base.lib
# Load head messages
load_messages $BIN_HOME $BIN_MESG $BIN_LANG "head"
title="${head_000} ${head_002}"
check_dependencies() {
# Verificar que 'dialog' esté instalado
if ! command -v dialog &>/dev/null; then
echo "${npm_001}"
exit 1
fi
# Verificar que 'sops' esté instalado
if ! command -v sops &>/dev/null; then
dialog --backtitle "$title" --title "${head_error}" --msgbox "El programa 'sops' no está instalado. Por favor, instálalo e inténtalo nuevamente." 7 50
exit 1
fi
}
encrypt_token() {
local sops_file="$1"
local token="$2"
local token_file="/tmp/cortana_token_$$"
local output_file="${HOME}/.cortana/cortana.sops.yaml"
# Ensure directory exists
mkdir -p "${HOME}/.cortana"
# Convert token to base64
local token_base64=$(echo -n "$token" | base64)
# Create temporary file with base64 encoded token
echo "$token_base64" > "$token_file"
# Encrypt using sops
if sops --encrypt "$token_file" > "$output_file"; then
# Ensure secure permissions
chmod 600 "$output_file"
else
rm -f "$token_file"
return 1
fi
# Clean up
rm -f "$token_file"
return 0
}
main() {
check_dependencies
# Ensure directory exists
mkdir -p "${HOME}/.cortana"
# Get SOPS config file path
sops_file=$(dialog --backtitle "$title" --stdout --title "SOPS Configuration" \
--fselect "$HOME/.cortana/cortana.sops.yaml" 8 60)
# Check if user canceled
if [ $? -ne 0 ]; then
clear
dialog --backtitle "$title" --title "${head_canceled}" --msgbox "${npm_008}" 6 40
exit 0
fi
# Check if file exists and ask for confirmation to overwrite
if [ -f "$sops_file" ]; then
dialog --backtitle "$title" --title "Confirmación" \
--yesno "El archivo $sops_file ya existe. ¿Desea sobrescribirlo?" 7 60
if [ $? -ne 0 ]; then
clear
dialog --backtitle "$title" --title "${head_canceled}" --msgbox "${npm_008}" 6 40
exit 0
fi
fi
# Get Cortana token
while true; do
token=$(dialog --backtitle "$title" --stdout --title "${npm_000} Token" \
--passwordbox "Ingrese su token de API de ${npm_000}:" 8 60)
# Check if user canceled
if [ $? -ne 0 ]; then
clear
dialog --backtitle "$title" --title "${head_canceled}" --msgbox "${npm_008}" 6 40
exit 0
fi
# Validate token
if [ -z "$token" ]; then
dialog --backtitle "$title" --title "${head_error}" --msgbox "El token no puede estar vacío. Por favor ingrese un token válido." 7 60
continue
fi
# Check token length (should be at least 64 characters)
if [ ${#token} -lt 64 ]; then
dialog --backtitle "$title" --title "${head_error}" --msgbox "El token parece ser demasiado corto. Verifique que haya copiado el token completo." 7 60
continue
fi
# Check token format (should contain alphanumeric characters and some special chars)
if ! [[ "$token" =~ ^[A-Za-z0-9\#\-\_\.]+$ ]]; then
dialog --backtitle "$title" --title "${head_warning}" --msgbox "El token contiene caracteres no estándar. Verifique que sea correcto." 7 60
fi
# Token is valid
break
done
# Clear screen before proceeding
clear
# Encrypt the token
if encrypt_token "$sops_file" "$token"; then
dialog --backtitle "$title" --title "${npm_014}" --msgbox "El token de ${npm_000} ha sido encriptado exitosamente en ${HOME}/.cortana/cortana.sops.yaml" 7 70
else
dialog --backtitle "$title" --title "${head_error}" --msgbox "Falló la encriptación del token de ${npm_000}." 7 50
exit 1
fi
}
# Execute main function
main
exit 0