Files
threshold/internal/cmd/commands/credentialscmd/ssh-private-key_funcs.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

179 lines
5.9 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package credentialscmd
import (
"errors"
"fmt"
"os"
"strings"
"github.com/hashicorp/boundary/api/credentials"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/go-secure-stdlib/parseutil"
"github.com/hashicorp/go-secure-stdlib/password"
"golang.org/x/crypto/ssh"
)
func init() {
extraSshPrivateKeyFlagsFunc = extraSshPrivateKeyFlagsFuncImpl
extraSshPrivateKeyActionsFlagsMapFunc = extraSshPrivateKeyActionsFlagsMapFuncImpl
extraSshPrivateKeyFlagsHandlingFunc = extraSshPrivateKeyFlagHandlingFuncImpl
}
type extraSshPrivateKeyCmdVars struct {
flagUsername string
flagPrivateKey string
flagPrivateKeyPassphrase string
}
func extraSshPrivateKeyActionsFlagsMapFuncImpl() map[string][]string {
flags := map[string][]string{
"create": {
usernameFlagName,
privateKeyFlagName,
privateKeyPassphraseFlagName,
},
}
flags["update"] = flags["create"]
return flags
}
func extraSshPrivateKeyFlagsFuncImpl(c *SshPrivateKeyCommand, set *base.FlagSets, _ *base.FlagSet) {
f := set.NewFlagSet("SSH Private Key Credential Options")
for _, name := range flagsSshPrivateKeyMap[c.Func] {
switch name {
case usernameFlagName:
f.StringVar(&base.StringVar{
Name: usernameFlagName,
Target: &c.flagUsername,
Usage: "The username associated with the credential.",
})
case privateKeyFlagName:
f.StringVar(&base.StringVar{
Name: privateKeyFlagName,
Target: &c.flagPrivateKey,
Usage: "The SSH private key associated with the credential. This can refer to a file on disk (file://) from which the value will be read or an env var (env://) from which the value will be read.",
})
case privateKeyPassphraseFlagName:
f.StringVar(&base.StringVar{
Name: privateKeyPassphraseFlagName,
Target: &c.flagPrivateKeyPassphrase,
Usage: "The passphrase associated with the SSH private key. This value is ignored if the private key does not require a passphrase or if no private key is supplied. This can refer to a file on disk (file://) from which the value will be read, or an env var (env://) from which the value will be read. Or, if left empty, if the key requires a passphrase it can be entered manually.",
})
}
}
}
func extraSshPrivateKeyFlagHandlingFuncImpl(c *SshPrivateKeyCommand, _ *base.FlagSets, opts *[]credentials.Option) bool {
switch c.flagUsername {
case "":
default:
*opts = append(*opts, credentials.WithSshPrivateKeyCredentialUsername(c.flagUsername))
}
// If private key not set (e.g. just a username update) then don't check
// either private key or passphrase
if c.flagPrivateKey == "" {
return true
}
// First parse private key
privateKey, err := parseutil.MustParsePath(c.flagPrivateKey)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Private key flag must be used with env:// or file:// syntax")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing private key flag: %v", err))
return false
}
// Now validate it
_, err = ssh.ParsePrivateKey([]byte(privateKey))
// If nil, parsed successfully without passphrase, ignore passphrase flag
if err == nil {
if c.flagPrivateKeyPassphrase != "" {
c.UI.Warn("Ignoring private key passphrase as private key is not encrypted.")
}
*opts = append(*opts, credentials.WithSshPrivateKeyCredentialPrivateKey(privateKey))
return true
}
// If an error but it's not a passphrase missing error, send the error back
if err.Error() != (&ssh.PassphraseMissingError{}).Error() {
c.UI.Error(fmt.Sprintf("Error parsing private key: %v", err))
return false
}
// Passphrase required
var privateKeyPassphrase string
switch c.flagPrivateKeyPassphrase {
case "":
fmt.Print("Please enter the passphrase for the private key (it will be hidden): ")
privateKeyPassphrase, err = password.Read(os.Stdin)
fmt.Print("\n")
if err != nil {
c.UI.Error(fmt.Sprintf("An error occurred attempting to read the passphrase. The raw error message is shown below but usually this is because you attempted to pipe a value into the command or you are executing outside of a terminal (TTY). The raw error was:\n\n%s", err.Error()))
return false
}
privateKeyPassphrase = strings.TrimSpace(privateKeyPassphrase)
default:
privateKeyPassphrase, err = parseutil.MustParsePath(c.flagPrivateKeyPassphrase)
switch {
case err == nil:
case errors.Is(err, parseutil.ErrNotParsed):
c.UI.Error("Private key passphrase flag must be used with env:// or file:// syntax")
return false
default:
c.UI.Error(fmt.Sprintf("Error parsing private key passphrase flag: %v", err))
return false
}
}
if _, err = ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(privateKeyPassphrase)); err != nil {
c.UI.Error(fmt.Sprintf("Error parsing private key passphrase: %v", err))
return false
}
*opts = append(*opts,
credentials.WithSshPrivateKeyCredentialPrivateKey(privateKey),
credentials.WithSshPrivateKeyCredentialPrivateKeyPassphrase(privateKeyPassphrase),
)
return true
}
func (c *SshPrivateKeyCommand) extraSshPrivateKeyHelpFunc(_ map[string]func() string) string {
var helpStr string
switch c.Func {
case "create":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary credentials create ssh-private-key -credential-store-id [options] [args]",
"",
" Create an SSH private key credential. Example:",
"",
` $ boundary credentials create ssh-private-key -credential-store-id csvlt_1234567890 -username user -private-key file:///home/user/.ssh/id_ed25519`,
"",
"",
})
case "update":
helpStr = base.WrapForHelpText([]string{
"Usage: boundary credentials update ssh-private-key [options] [args]",
"",
" Update an SSH private key credential given its ID. Example:",
"",
` $ boundary credentials update ssh-private-key -id clvlt_1234567890 -name devops -description "For DevOps usage"`,
"",
"",
})
}
return helpStr + c.Flags().Help()
}