Skip to content

Add initial version of the handshake command #1402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/step/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
_ "github.com/smallstep/cli/command/oauth"
_ "github.com/smallstep/cli/command/path"
_ "github.com/smallstep/cli/command/ssh"
_ "github.com/smallstep/cli/command/tls"

Check failure on line 46 in cmd/step/main.go

View workflow job for this annotation

GitHub Actions / ci / lint / lint

could not import github.com/smallstep/cli/command/tls (-: # github.com/smallstep/cli/command/tls
)

// Version is set by an LDFLAG at build time representing the git tag or commit
Expand Down
2 changes: 1 addition & 1 deletion command/certificate/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func fingerprintAction(ctx *cli.Context) error {
return err
}

switch addr, isURL, err := trimURL(crtFile); {
switch addr, isURL, err := utils.TrimURL(crtFile); {
case err != nil:
return err
case isURL:
Expand Down
2 changes: 1 addition & 1 deletion command/certificate/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func inspectAction(ctx *cli.Context) error {
return errs.IncompatibleFlagWithFlag(ctx, "short", "format "+format)
}

switch addr, isURL, err := trimURL(crtFile); {
switch addr, isURL, err := utils.TrimURL(crtFile); {
case err != nil:
return err
case isURL:
Expand Down
3 changes: 2 additions & 1 deletion command/certificate/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/smallstep/zlint"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils"
)

func lintCommand() cli.Command {
Expand Down Expand Up @@ -103,7 +104,7 @@ func lintAction(ctx *cli.Context) error {
insecure = ctx.Bool("insecure")
block *pem.Block
)
switch addr, isURL, err := trimURL(crtFile); {
switch addr, isURL, err := utils.TrimURL(crtFile); {
case err != nil:
return err
case isURL:
Expand Down
3 changes: 2 additions & 1 deletion command/certificate/needsRenewal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.step.sm/crypto/pemutil"

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils"
)

const defaultPercentUsedThreshold = 66
Expand Down Expand Up @@ -157,7 +158,7 @@ func needsRenewalAction(ctx *cli.Context) error {
)

var certs []*x509.Certificate
switch addr, isURL, err := trimURL(certFile); {
switch addr, isURL, err := utils.TrimURL(certFile); {
case err != nil:
return errs.NewExitError(err, 255)
case isURL:
Expand Down
33 changes: 0 additions & 33 deletions command/certificate/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"crypto/tls"
"crypto/x509"
"net"
"net/url"
"strconv"
"strings"

"github.com/pkg/errors"
"go.step.sm/crypto/x509util"
Expand Down Expand Up @@ -63,33 +60,3 @@ func getPeerCertificates(addr, serverName, roots string, insecure bool) ([]*x509
conn.Close()
return conn.ConnectionState().PeerCertificates, nil
}

// trimURL returns the host[:port] if the input is a URL, otherwise returns an
// empty string (and 'isURL:false').
//
// If the URL is valid and no port is specified, the default port determined
// by the URL prefix is used.
//
// Examples:
// trimURL("https://smallstep.com/onboarding") -> "smallstep.com:443", true, nil
// trimURL("https://ca.smallSTEP.com:8080") -> "ca.smallSTEP.com:8080", true, nil
// trimURL("./certs/root_ca.crt") -> "", false, nil
// trimURL("hTtPs://sMaLlStEp.cOm") -> "sMaLlStEp.cOm:443", true, nil
// trimURL("hTtPs://sMaLlStEp.cOm hello") -> "", false, err{"invalid url"}
func trimURL(ref string) (string, bool, error) {
tmp := strings.ToLower(ref)
for prefix := range urlPrefixes {
if strings.HasPrefix(tmp, prefix) {
u, err := url.Parse(ref)
if err != nil {
return "", false, errors.Wrapf(err, "error parsing URL '%s'", ref)
}
if _, _, err := net.SplitHostPort(u.Host); err != nil {
port := strconv.FormatUint(uint64(urlPrefixes[prefix]), 10)
u.Host = net.JoinHostPort(u.Host, port)
}
return u.Host, true, nil
}
}
return "", false, nil
}
31 changes: 0 additions & 31 deletions command/certificate/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,6 @@ import (
"github.com/smallstep/assert"
)

func TestTrimURL(t *testing.T) {
type newTest struct {
input, host string
isURL bool
err error
}
tests := map[string]newTest{
"true-http": {"https://smallstep.com", "smallstep.com:443", true, nil},
"true-tcp": {"tcp://smallstep.com:8080", "smallstep.com:8080", true, nil},
"true-tls": {"tls://smallstep.com/onboarding", "smallstep.com:443", true, nil},
"false": {"./certs/root_ca.crt", "", false, nil},
"false-err": {"https://google.com hello", "", false, errors.New("error parsing URL 'https://google.com hello'")},
"true-http-case": {"hTtPs://sMaLlStEp.cOm", "sMaLlStEp.cOm:443", true, nil},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
host, isURL, err := trimURL(tc.input)
assert.Equals(t, tc.host, host)
assert.Equals(t, tc.isURL, isURL)
if err != nil {
if assert.NotNil(t, tc.err) {
assert.HasPrefix(t, err.Error(), tc.err.Error())
}
} else {
assert.Nil(t, tc.err)
}
})
}
}

func TestGetPeerCertificateServerName(t *testing.T) {
host := "smallstep.com"
serverName := host
Expand Down
3 changes: 2 additions & 1 deletion command/certificate/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/internal/crlutil"
"github.com/smallstep/cli/utils"
)

func verifyCommand() cli.Command {
Expand Down Expand Up @@ -170,7 +171,7 @@ func verifyAction(ctx *cli.Context) error {
httpClient *http.Client
)

switch addr, isURL, err := trimURL(crtFile); {
switch addr, isURL, err := utils.TrimURL(crtFile); {
case err != nil:
return err
case isURL:
Expand Down
Loading
Loading