Skip to content
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

feat(cloud): Add create command for certificates #1662

Merged
Merged
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
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require (
k8s.io/apimachinery v0.30.1
k8s.io/apiserver v0.29.3
oras.land/oras-go/v2 v2.5.0
sdk.kraft.cloud v0.5.9-0.20240430124855-83577e6062d6
sdk.kraft.cloud v0.5.9-0.20240524113046-d2973ab7c44e
sigs.k8s.io/kustomize/kyaml v0.17.0
)

Expand Down Expand Up @@ -262,11 +262,11 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/genproto v0.0.0-20240311173647-c811ad7063a7 // indirect
Expand Down
15 changes: 8 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1335,8 +1335,8 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -1418,8 +1418,8 @@ golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1559,8 +1559,9 @@ golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1797,8 +1798,8 @@ oras.land/oras-go/v2 v2.5.0/go.mod h1:z4eisnLP530vwIOUOJeBIj0aGI0L1C3d53atvCBqZH
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sdk.kraft.cloud v0.5.9-0.20240430124855-83577e6062d6 h1:QNW7+eLtiNtvFG7oqxXNH/TVTbMxcYdCW13d1aCFusg=
sdk.kraft.cloud v0.5.9-0.20240430124855-83577e6062d6/go.mod h1:FX/8C4OFJPz+wnExu3WB/KnvEYsMuuyGEprNTB+LhxQ=
sdk.kraft.cloud v0.5.9-0.20240524113046-d2973ab7c44e h1:EigHhuXouyyccfE9e9gSm1y+12gq4GjO7DUtuTnPVjE=
sdk.kraft.cloud v0.5.9-0.20240524113046-d2973ab7c44e/go.mod h1:d44Ht2AeYURqTjCOw5cJCewGFaM8hh3gy2CeekwKa20=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.14/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 h1:TgtAeesdhpm2SGwkQasmbeqDo8th5wOBA5h/AjTKA4I=
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/kraft/cloud/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"kraftkit.sh/cmdfactory"

"kraftkit.sh/internal/cli/kraft/cloud/certificate/create"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/get"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/list"
"kraftkit.sh/internal/cli/kraft/cloud/certificate/remove"
Expand All @@ -35,6 +36,7 @@ func NewCmd() *cobra.Command {
panic(err)
}

cmd.AddCommand(create.NewCmd())
cmd.AddCommand(list.NewCmd())
cmd.AddCommand(remove.NewCmd())
cmd.AddCommand(get.NewCmd())
Expand Down
143 changes: 143 additions & 0 deletions internal/cli/kraft/cloud/certificate/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package create

import (
"context"
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"

kraftcloud "sdk.kraft.cloud"
kccertificates "sdk.kraft.cloud/certificates"

"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
)

type CreateOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kccertificates.CertificatesService `noattribute:"true"`
Metro string `noattribute:"true"`
Chain string `local:"true" long:"chain" short:"C" usage:"The chain of the certificate"`
CN string `local:"true" long:"cn" short:"c" usage:"The common name of the certificate"`
Name string `local:"true" size:"name" short:"n" usage:"The name of the certificate"`
PKey string `local:"true" long:"pkey" short:"p" usage:"The private key of the certificate in PEM format"`
Token string `noattribute:"true"`
}

// Create a KraftCloud certificate.
func Create(ctx context.Context, opts *CreateOptions) (*kccertificates.CreateResponseItem, error) {
var err error

if opts == nil {
opts = &CreateOptions{}
}

if opts.Auth == nil {
opts.Auth, err = config.GetKraftCloudAuthConfig(ctx, opts.Token)
if err != nil {
return nil, fmt.Errorf("could not retrieve credentials: %w", err)
}
}

if opts.Client == nil {
opts.Client = kraftcloud.NewCertificatesClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}

if _, err := os.Stat(opts.PKey); err != nil {
if os.IsNotExist(err) {
log.G(ctx).Info("reading private key from argument")
} else {
return nil, fmt.Errorf("could not read private key: %w", err)
}
} else {
b, err := os.ReadFile(opts.PKey)
if err != nil {
return nil, fmt.Errorf("could not read private key: %w", err)
}

opts.PKey = string(b)
}

createResp, err := opts.Client.WithMetro(opts.Metro).Create(ctx, &kccertificates.CreateRequest{
Chain: opts.Chain,
CN: opts.CN,
Name: opts.Name,
PKey: opts.PKey,
})
if err != nil {
return nil, fmt.Errorf("creating certificate: %w", err)
}
create, err := createResp.FirstOrErr()
if err != nil {
return nil, fmt.Errorf("creating certificate: %w", err)
}

return create, nil
}

func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&CreateOptions{}, cobra.Command{
Short: "Create a certificate",
Use: "create [FLAGS]",
Args: cobra.NoArgs,
Aliases: []string{"crt"},
Long: heredoc.Doc(`
Create a new certificate.
`),
Example: heredoc.Doc(`
# Create a new certificate with a given common name, private key file and chain.
$ kraft cloud certificate create --name my-cert --cname '*.example.com' --pkey 'private-key.pem' --chain 'chain.pem'
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-vol",
},
})
if err != nil {
panic(err)
}

return cmd
}

func (opts *CreateOptions) Pre(cmd *cobra.Command, _ []string) error {
if opts.CN == "" {
return fmt.Errorf("common name (CN) is required")
}

if opts.PKey == "" {
return fmt.Errorf("private key is required")
}

if opts.Chain == "" {
return fmt.Errorf("chain is required")
}

err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token)
if err != nil {
return fmt.Errorf("could not populate metro and token: %w", err)
}

return nil
}

func (opts *CreateOptions) Run(ctx context.Context, _ []string) error {
certificate, err := Create(ctx, opts)
if err != nil {
return fmt.Errorf("could not create certificate: %w", err)
}

_, err = fmt.Fprintln(iostreams.G(ctx).Out, certificate.Name)
return err
}
Loading