Skip to content

Commit c2fb7a3

Browse files
authored
Merge pull request #4 from smallstep/attest
Add step-kms-plugin attest command
2 parents 441f501 + 2503504 commit c2fb7a3

File tree

8 files changed

+113
-11
lines changed

8 files changed

+113
-11
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Go
1919
uses: actions/setup-go@v3
2020
with:
21-
go-version: "1.18"
21+
go-version: '1.19'
2222
- name: golangci-lint
2323
uses: golangci/golangci-lint-action@v2
2424
with:
@@ -77,7 +77,7 @@ jobs:
7777
- name: Set up Go
7878
uses: actions/setup-go@v3
7979
with:
80-
go-version: 1.18
80+
go-version: '1.19'
8181
- name: release dry run
8282
run: make release-dry-run
8383
- name: setup release environment

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup Go
2121
uses: actions/setup-go@v3
2222
with:
23-
go-version: "1.18"
23+
go-version: '1.19'
2424
- name: golangci-lint
2525
uses: golangci/golangci-lint-action@v3
2626
with:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PKG?=github.com/smallstep/step-kms-plugin
22
BINNAME?=step-kms-plugin
3-
GOLANG_CROSS_VERSION?=v1.18.3
3+
GOLANG_CROSS_VERSION?=v1.19.0
44

55
# Set V to 1 for verbose output from the Makefile
66
Q=$(if $V,,@)

cmd/attest.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2022 Smallstep Labs, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package cmd
15+
16+
import (
17+
"context"
18+
"encoding/pem"
19+
"errors"
20+
"fmt"
21+
"os"
22+
23+
"github.com/smallstep/step-kms-plugin/internal/flagutil"
24+
"github.com/spf13/cobra"
25+
"go.step.sm/crypto/kms"
26+
"go.step.sm/crypto/kms/apiv1"
27+
"go.step.sm/crypto/pemutil"
28+
)
29+
30+
// attestCmd represents the attest command
31+
var attestCmd = &cobra.Command{
32+
Use: "attest <uri>",
33+
Short: "create an attestation certificate",
34+
Long: `This command, if the KMS supports it, it prints an attestation certificate or an endorsement key.
35+
36+
Currently this command is only supported on YubiKeys.`,
37+
Example: ` # Get the attestation certificate from a YubiKey:
38+
step-kms-plugin attest yubikey:slot-id=9c`,
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
if len(args) != 1 {
41+
return showUsageErr(cmd)
42+
}
43+
44+
flags := cmd.Flags()
45+
kuri := flagutil.MustString(flags, "kms")
46+
if kuri == "" {
47+
kuri = args[0]
48+
}
49+
50+
km, err := kms.New(context.Background(), apiv1.Options{
51+
URI: kuri,
52+
})
53+
if err != nil {
54+
return fmt.Errorf("failed to load key manager: %w", err)
55+
}
56+
defer km.Close()
57+
58+
attester, ok := km.(apiv1.Attester)
59+
if !ok {
60+
return fmt.Errorf("%s does not implement Attester", kuri)
61+
}
62+
63+
resp, err := attester.CreateAttestation(&apiv1.CreateAttestationRequest{
64+
Name: args[0],
65+
})
66+
if err != nil {
67+
return fmt.Errorf("failed to attest: %w", err)
68+
}
69+
70+
switch {
71+
case resp.Certificate != nil:
72+
if err := pem.Encode(os.Stdout, &pem.Block{
73+
Type: "CERTIFICATE",
74+
Bytes: resp.Certificate.Raw,
75+
}); err != nil {
76+
return fmt.Errorf("failed to encode certificate: %w", err)
77+
}
78+
for _, c := range resp.CertificateChain {
79+
if err := pem.Encode(os.Stdout, &pem.Block{
80+
Type: "CERTIFICATE",
81+
Bytes: c.Raw,
82+
}); err != nil {
83+
return fmt.Errorf("failed to encode certificate chain: %w", err)
84+
}
85+
}
86+
return nil
87+
case resp.PublicKey != nil:
88+
block, err := pemutil.Serialize(resp.PublicKey)
89+
if err != nil {
90+
return err
91+
}
92+
return pem.Encode(os.Stdout, block)
93+
default:
94+
return errors.New("failed to create attestation: unsupported response")
95+
}
96+
},
97+
}
98+
99+
func init() {
100+
rootCmd.AddCommand(attestCmd)
101+
attestCmd.SilenceUsage = true
102+
}

cmd/key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,

cmd/sign.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -24,7 +24,7 @@ import (
2424
"encoding/base64"
2525
"encoding/hex"
2626
"fmt"
27-
"io/ioutil"
27+
"io"
2828
"os"
2929

3030
"github.com/smallstep/step-kms-plugin/internal/flagutil"
@@ -130,7 +130,7 @@ digest of the data file for you.`,
130130
}
131131
default:
132132
// Data passed by stdin is in binary form.
133-
digest, err = ioutil.ReadAll(os.Stdin)
133+
digest, err = io.ReadAll(os.Stdin)
134134
if err != nil {
135135
return err
136136
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
github.com/spf13/cobra v1.5.0
77
github.com/spf13/pflag v1.0.5
8-
go.step.sm/crypto v0.17.0
8+
go.step.sm/crypto v0.19.0
99
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3
1010
)
1111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
325325
go.step.sm/cli-utils v0.7.3 h1:IA12IaiXVCI18yOFVQuvMpyvjL8wuwUn1yO+KhAVAr0=
326326
go.step.sm/cli-utils v0.7.3/go.mod h1:RJRwbBLqzs5nrepQLAV9FuT3fVpWz66tKzLIB7Izpfk=
327327
go.step.sm/crypto v0.9.0/go.mod h1:+CYG05Mek1YDqi5WK0ERc6cOpKly2i/a5aZmU1sfGj0=
328-
go.step.sm/crypto v0.17.0 h1:qaLUbWygcMRMxrsz91jL5ytHIsIMABFYX6TkU+V8Pq8=
329-
go.step.sm/crypto v0.17.0/go.mod h1:2oZdJ4ZUqPv5q8wz6yN4Qfsdcu2+eRaob4q1E5Azavs=
328+
go.step.sm/crypto v0.19.0 h1:WxjUDeTDpuPZ1IR3v6c4jc6WdlQlS5IYYQBhfnG5uW0=
329+
go.step.sm/crypto v0.19.0/go.mod h1:qZ+pNU1nV+THwP7TPTNCRMRr9xrRURhETTAK7U5psfw=
330330
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
331331
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
332332
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

0 commit comments

Comments
 (0)