-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented issue cmd to issue mTLS certs locally. Cert validity parsing from timespec for notBefore and notAfter.
- Loading branch information
Showing
6 changed files
with
256 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/RealImage/bifrost/cafiles" | ||
"github.com/RealImage/bifrost/tinyca" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var ( | ||
notBefore cli.Timestamp | ||
notAfter cli.Timestamp | ||
|
||
issueCmd = &cli.Command{ | ||
Name: "issue", | ||
Flags: []cli.Flag{ | ||
caCertFlag, | ||
caPrivKeyFlag, | ||
&cli.TimestampFlag{ | ||
Name: "not-before", | ||
Usage: "issue certificates valid from `TIMESTAMP`", | ||
Aliases: []string{"before"}, | ||
EnvVars: []string{"NOT_BEFORE"}, | ||
Value: cli.NewTimestamp(time.Now()), | ||
Destination: ¬Before, | ||
}, | ||
&cli.TimestampFlag{ | ||
Name: "not-after", | ||
Usage: "issue certificates valid until `TIMESTAMP`", | ||
Aliases: []string{"after"}, | ||
EnvVars: []string{"NOT_AFTER"}, | ||
Value: cli.NewTimestamp(time.Now().AddDate(0, 0, 1)), | ||
Destination: ¬After, | ||
var issueCmd = &cli.Command{ | ||
Name: "issue", | ||
Flags: []cli.Flag{ | ||
caCertFlag, | ||
caPrivKeyFlag, | ||
clientPrivKeyFlag, | ||
notBeforeFlag, | ||
notAfterFlag, | ||
outputFlag, | ||
}, | ||
|
||
Action: func(cliCtx *cli.Context) error { | ||
ctx := cliCtx.Context | ||
caCert, caKey, err := cafiles.GetCertKey(ctx, caCertUri, caPrivKeyUri) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error reading cert/key: %s", err), 1) | ||
} | ||
|
||
ca, err := tinyca.New(caCert, caKey) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error creating CA: %s", err), 1) | ||
} | ||
|
||
clientKey, err := cafiles.GetPrivateKey(ctx, clientPrivKeyUri) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error reading client key: %s", err), 1) | ||
} | ||
|
||
csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{ | ||
Subject: pkix.Name{ | ||
Organization: []string{caCert.Namespace.String()}, | ||
CommonName: clientKey.UUID(caCert.Namespace).String(), | ||
}, | ||
}, | ||
|
||
Action: func(cliCtx *cli.Context) error { | ||
ctx := cliCtx.Context | ||
cert, key, err := cafiles.GetCertKey(ctx, caCertUri, caPrivKeyUri) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error reading cert/key: %s", err), 1) | ||
} | ||
|
||
_, err = tinyca.New(cert, key) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error creating CA: %s", err), 1) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
) | ||
}, clientKey) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error creating certificate request: %s", err), 1) | ||
} | ||
|
||
notBefore, notAfter, err := tinyca.ParseValidity(notBeforeTime, notAfterTime) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error parsing validity: %s", err), 1) | ||
} | ||
|
||
template := tinyca.TLSClientCertTemplate(notBefore, notAfter) | ||
|
||
cert, err := ca.IssueCertificate(csr, template) | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error issuing certificate: %s", err), 1) | ||
} | ||
|
||
out, err := getOutputWriter() | ||
if err != nil { | ||
return cli.Exit(fmt.Sprintf("Error getting output writer: %s", err), 1) | ||
} | ||
|
||
block := &pem.Block{ | ||
Type: "CERTIFICATE", | ||
Bytes: cert, | ||
} | ||
|
||
fmt.Fprint(out, string(pem.EncodeToMemory(block))) | ||
|
||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tinyca | ||
|
||
import ( | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
func TLSClientCertTemplate(nb, na time.Time) *x509.Certificate { | ||
return &x509.Certificate{ | ||
NotBefore: nb, | ||
NotAfter: na, | ||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
} | ||
|
||
func CACertTemplate(nb, na time.Time, ns, id uuid.UUID) *x509.Certificate { | ||
return &x509.Certificate{ | ||
Subject: pkix.Name{ | ||
Organization: []string{ns.String()}, | ||
CommonName: id.String(), | ||
}, | ||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, | ||
BasicConstraintsValid: true, | ||
IsCA: true, | ||
MaxPathLenZero: true, | ||
} | ||
} |
Oops, something went wrong.