示例#1
0
文件: cmdUnlock.go 项目: rhoml/awsenv
func actionCmdUnlock(cmd *cobra.Command, args []string) {
	var pwd *security.DatabasePassword
	var err error

	if len(cfg.Password) > 0 {
		pwd = security.LoadDatabasePasswordFromInput(cfg.Password)
	} else {
		line, err := speakeasy.Ask("Password: "******"%s.lock", cfg.Database))
	if err != nil {
		log.Errorf("Unable to spawn lockagent: %s", err)
		return
	}

	fmt.Println("Database unlocked.")
}
示例#2
0
文件: main.go 项目: rhoml/awsenv
func main() {
	// Do not route special commands into cobra logic
	if len(os.Args) > 1 {
		switch os.Args[1] {
		case "lockagent":
			runLockagent()
			os.Exit(0)
		}
	}

	app := cobra.Command{
		Use:   "awsenv",
		Short: "manage different AWS envs on your system",
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			if cfg.Debug {
				log.SetLevel(log.DebugLevel)
			}

			// Load the password if command is not unlock
			if !strings.Contains("unlock version", cmd.Name()) {

				if len(cfg.Password) > 0 {
					// If a password was provided, use that one
					password = security.LoadDatabasePasswordFromInput(cfg.Password)
				} else {
					// If the token file exists a lockagent should be running and we can use
					// the password stored in that logagent
					filename := fmt.Sprintf("%s.lock", cfg.Database)
					if _, err := os.Stat(filename); os.IsNotExist(err) {
						log.Errorf("No password is available. Use 'unlock' or provide --password.")
						os.Exit(1)
					}
					pwd, err := security.LoadDatabasePasswordFromLockagent(filename)
					if err != nil {
						log.Errorf("Could not load password from lock-file:\n%s", err)
						os.Exit(1)
					}
					password = pwd
				}

				// As we got a password now try to load the database with that password or
				// Create a new one if the encrypted storage file is not available
				if _, err := os.Stat(cfg.Database); os.IsNotExist(err) {
					awsCredentials = credentials.New(cfg.Database, password)
				} else {
					s, err := credentials.FromFile(cfg.Database, password)
					if err != nil {
						log.Error("Unable to read credential database")
						os.Exit(1)
					}
					awsCredentials = s
				}
			}
		},
	}

	app.PersistentFlags().StringVarP(&cfg.Password, "password", "p", os.Getenv("AWSENV_PASSWORD"), "password to en/decrypt the database")
	app.PersistentFlags().StringVar(&cfg.Database, "database", strings.Join([]string{os.Getenv("HOME"), ".config/awsenv"}, "/"), "storage location of the database")
	app.PersistentFlags().BoolVar(&cfg.Debug, "debug", false, "print debug information")

	app.AddCommand(
		getCmdAdd(),
		getCmdConsole(),
		getCmdDelete(),
		getCmdGet(),
		getCmdList(),
		getCmdLock(),
		getCmdShell(),
		getCmdPrompt(),
		getCmdUnlock(),
		getCmdVersion(),
	)

	_ = app.Execute()
}