| 1 |
package cautils |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/x509" |
| 5 |
"net/http" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/smallstep/certificates/api" |
| 9 |
"github.com/smallstep/certificates/ca" |
| 10 |
"github.com/smallstep/certificates/pki" |
| 11 |
"github.com/smallstep/cli/errs" |
| 12 |
"github.com/smallstep/cli/flags" |
| 13 |
"github.com/urfave/cli" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type CaClient interface { |
| 19 |
Sign(req *api.SignRequest) (*api.SignResponse, error) |
| 20 |
Renew(tr http.RoundTripper) (*api.SignResponse, error) |
| 21 |
Revoke(req *api.RevokeRequest, tr http.RoundTripper) (*api.RevokeResponse, error) |
| 22 |
SSHSign(req *api.SSHSignRequest) (*api.SSHSignResponse, error) |
| 23 |
SSHRenew(req *api.SSHRenewRequest) (*api.SSHRenewResponse, error) |
| 24 |
SSHRekey(req *api.SSHRekeyRequest) (*api.SSHRekeyResponse, error) |
| 25 |
SSHRevoke(req *api.SSHRevokeRequest) (*api.SSHRevokeResponse, error) |
| 26 |
SSHRoots() (*api.SSHRootsResponse, error) |
| 27 |
SSHFederation() (*api.SSHRootsResponse, error) |
| 28 |
SSHConfig(req *api.SSHConfigRequest) (*api.SSHConfigResponse, error) |
| 29 |
SSHCheckHost(principal string, token string) (*api.SSHCheckPrincipalResponse, error) |
| 30 |
SSHGetHosts() (*api.SSHGetHostsResponse, error) |
| 31 |
SSHBastion(req *api.SSHBastionRequest) (*api.SSHBastionResponse, error) |
| 32 |
Version() (*api.VersionResponse, error) |
| 33 |
GetRootCAs() *x509.CertPool |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
func NewClient(ctx *cli.Context, opts ...ca.ClientOption) (CaClient, error) { |
| 39 |
if ctx.Bool("offline") { |
| 40 |
caConfig := ctx.String("ca-config") |
| 41 |
if caConfig == "" { |
| 42 |
return nil, errs.InvalidFlagValue(ctx, "ca-config", "", "") |
| 43 |
} |
| 44 |
return NewOfflineCA(caConfig) |
| 45 |
} |
| 46 |
|
| 47 |
caURL, err := flags.ParseCaURL(ctx) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
root := ctx.String("root") |
| 52 |
if len(root) == 0 { |
| 53 |
root = pki.GetRootCAPath() |
| 54 |
if _, err := os.Stat(root); err != nil { |
| 55 |
return nil, errs.RequiredFlag(ctx, "root") |
| 56 |
} |
| 57 |
} |
| 58 |
opts = append([]ca.ClientOption{ca.WithRootFile(root)}, opts...) |
| 59 |
return ca.NewClient(caURL, opts...) |
| 60 |
} |