Skip to content

Commit

Permalink
Merge branch 'release/1.9.x' into ui/backport-pr-15824-1.9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Monkeychip committed Jun 27, 2022
2 parents 853dc1d + 7a4a67b commit 211f4d2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CMD ["server", "-dev"]


## UBI DOCKERFILE ##
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.5 as ubi
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.6 as ubi

ARG BIN_NAME
# PRODUCT_VERSION is the version built dist/$TARGETOS/$TARGETARCH/$BIN_NAME,
Expand Down
5 changes: 4 additions & 1 deletion builtin/logical/ssh/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,8 @@ func TestBackend_DefExtTemplatingEnabled(t *testing.T) {
"default_extensions_template": true,
"default_extensions": map[string]interface{}{
"login@foobar.com": "{{identity.entity.aliases." + userpassAccessor + ".name}}",
"login@foobar2.com": "{{identity.entity.aliases." + userpassAccessor + ".name}}, " +
"{{identity.entity.aliases." + userpassAccessor + ".name}}_foobar",
},
})
if err != nil {
Expand All @@ -1386,7 +1388,8 @@ func TestBackend_DefExtTemplatingEnabled(t *testing.T) {
}

defaultExtensionPermissions := map[string]string{
"login@foobar.com": testUserName,
"login@foobar.com": testUserName,
"login@foobar2.com": fmt.Sprintf("%s, %s_foobar", testUserName, testUserName),
}

err = validateSSHCertificate(parsedKey.(*ssh.Certificate), sshKeyID, ssh.UserCert, []string{"tuber"}, map[string]string{}, defaultExtensionPermissions, 16*time.Hour)
Expand Down
6 changes: 4 additions & 2 deletions builtin/logical/ssh/path_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type creationBundle struct {
Extensions map[string]string
}

var containsTemplateRegex = regexp.MustCompile(`{{.+?}}`)

func pathSign(b *backend) *framework.Path {
return &framework.Path{
Pattern: "sign/" + framework.GenericNameWithAtRegex("role"),
Expand Down Expand Up @@ -220,7 +222,7 @@ func (b *backend) calculateValidPrincipals(data *framework.FieldData, req *logic
for _, principal := range strutil.RemoveDuplicates(strutil.ParseStringSlice(principalsAllowedByRole, ","), false) {
if role.AllowedUsersTemplate {
// Look for templating markers {{ .* }}
matched, _ := regexp.MatchString(`{{.+?}}`, principal)
matched := containsTemplateRegex.MatchString(principal)
if matched {
if req.EntityID != "" {
// Retrieve principal based on template + entityID from request.
Expand Down Expand Up @@ -384,7 +386,7 @@ func (b *backend) calculateExtensions(data *framework.FieldData, req *logical.Re
if role.DefaultExtensionsTemplate {
for extensionKey, extensionValue := range role.DefaultExtensions {
// Look for templating markers {{ .* }}
matched, _ := regexp.MatchString(`^{{.+?}}$`, extensionValue)
matched := containsTemplateRegex.MatchString(extensionValue)
if matched {
if req.EntityID != "" {
// Retrieve extension value based on template + entityID from request.
Expand Down
3 changes: 3 additions & 0 deletions changelog/15946.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core/seal: Fix possible keyring truncation when using the file backend.
```
3 changes: 3 additions & 0 deletions changelog/16018.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
secrets/ssh: Allow additional text along with a template definition in defaultExtension value fields.
```
11 changes: 8 additions & 3 deletions sdk/physical/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er

// JSON encode the entry and write it
fullPath := filepath.Join(path, key)
tempPath := fullPath + ".temp"
f, err := os.OpenFile(
fullPath,
tempPath,
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
0o600)
if err != nil {
Expand All @@ -262,6 +263,10 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
})
f.Close()
if encErr == nil {
err = os.Rename(tempPath, fullPath)
if err != nil {
return err
}
return nil
}

Expand All @@ -270,15 +275,15 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
// See if we ended up with a zero-byte file and if so delete it, might be a
// case of disk being full but the file info is in metadata that is
// reserved.
fi, err := os.Stat(fullPath)
fi, err := os.Stat(tempPath)
if err != nil {
return encErr
}
if fi == nil {
return encErr
}
if fi.Size() == 0 {
os.Remove(fullPath)
os.Remove(tempPath)
}
return encErr
}
Expand Down
92 changes: 56 additions & 36 deletions website/content/docs/platform/k8s/helm/examples/standalone-tls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,42 @@ This example can be used to set up a single server Vault cluster using TLS.

## 1. Create key & certificate using Kubernetes CA

There are three variables that will be used in this example.
There are four variables that will be used in this example.

```bash
# SERVICE is the name of the Vault service in Kubernetes.
# It does not have to match the actual running service, though it may help for consistency.
SERVICE=vault-server-tls
export SERVICE=vault-server-tls

# NAMESPACE where the Vault service is running.
NAMESPACE=vault-namespace
export NAMESPACE=vault-namespace

# SECRET_NAME to create in the Kubernetes secrets store.
SECRET_NAME=vault-server-tls
export SECRET_NAME=vault-server-tls

# TMPDIR is a temporary working directory.
TMPDIR=/tmp
export TMPDIR=/tmp

# CSR_NAME will be the name of our certificate signing request as seen by Kubernetes.
export CSR_NAME=vault-csr
```

1. Create a key for Kubernetes to sign.

```bash
openssl genrsa -out ${TMPDIR}/vault.key 2048
```shell-session
$ openssl genrsa -out ${TMPDIR}/vault.key 2048
Generating RSA private key, 2048 bit long modulus
...................................................................................................+++
...............+++
e is 65537 (0x10001)
```

2. Create a Certificate Signing Request (CSR).

1. Create a file `${TMPDIR}/csr.conf` with the following contents:

```
$ cat <<EOF >${TMPDIR}/csr.conf
```bash
cat <<EOF >${TMPDIR}/csr.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
Expand All @@ -67,86 +74,99 @@ TMPDIR=/tmp
2. Create a CSR.
```bash
openssl req -new -key ${TMPDIR}/vault.key -subj "/CN=${SERVICE}.${NAMESPACE}.svc" -out ${TMPDIR}/server.csr -config ${TMPDIR}/csr.conf
```shell-session
$ openssl req -new -key ${TMPDIR}/vault.key \
-subj "/O=system:nodes/CN=system:node:${SERVICE}.${NAMESPACE}.svc" \
-out ${TMPDIR}/server.csr \
-config ${TMPDIR}/csr.conf
```
3. Create the certificate
1. Create a file `${TMPDIR}/csr.yaml` with the following contents:
```
$ export CSR_NAME=vault-csr
$ cat <<EOF >${TMPDIR}/csr.yaml
apiVersion: certificates.k8s.io/v1beta1
```bash
cat <<EOF >${TMPDIR}/csr.yaml
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: ${CSR_NAME}
spec:
groups:
- system:authenticated
request: $(cat ${TMPDIR}/server.csr | base64 | tr -d '\n')
request: $(cat ${TMPDIR}/server.csr | base64 | tr -d '\r\n')
signerName: kubernetes.io/kubelet-serving
usages:
- digital signature
- key encipherment
- server auth
EOF
```
-> `CSR_NAME` can be any name you want. It's the name of the CSR as seen by Kubernetes

2. Send the CSR to Kubernetes.
```bash
kubectl create -f ${TMPDIR}/csr.yaml
```shell-session
$ kubectl create -f ${TMPDIR}/csr.yaml
certificatesigningrequest.certificates.k8s.io/vault-csr created
```
-> If this process is automated, you may need to wait to ensure the CSR has been received and stored:
`kubectl get csr ${CSR_NAME}`
3. Approve the CSR in Kubernetes.
```bash
kubectl certificate approve ${CSR_NAME}
```shell-session
$ kubectl certificate approve ${CSR_NAME}
certificatesigningrequest.certificates.k8s.io/vault-csr approved
```
4. Verify that the certificate was approved and issued.
```shell-session
$ kubectl get csr ${CSR_NAME}
NAME AGE SIGNERNAME REQUESTOR CONDITION
vault-csr 1m13s kubernetes.io/kubelet-serving kubernetes-admin Approved,Issued
```
## 2. Store key, cert, and Kubernetes CA into Kubernetes secrets store
1. Retrieve the certificate.
```bash
serverCert=$(kubectl get csr ${CSR_NAME} -o jsonpath='{.status.certificate}')
```shell-session
$ serverCert=$(kubectl get csr ${CSR_NAME} -o jsonpath='{.status.certificate}')
```
-> If this process is automated, you may need to wait to ensure the certificate has been created.
If it hasn't, this will return an empty string.
2. Write the certificate out to a file.
```bash
echo "${serverCert}" | openssl base64 -d -A -out ${TMPDIR}/vault.crt
```shell-session
$ echo "${serverCert}" | openssl base64 -d -A -out ${TMPDIR}/vault.crt
```
3. Retrieve Kubernetes CA.
```bash
kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}' | base64 -d > ${TMPDIR}/vault.ca
```shell-session
$ kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}' | base64 -d > ${TMPDIR}/vault.ca
```
4. Create the namespace.
```bash
kubectl create namespace ${NAMESPACE}
```shell-session
$ kubectl create namespace ${NAMESPACE}
namespace/vault-namespace created
```
5. Store the key, cert, and Kubernetes CA into Kubernetes secrets.
```bash
kubectl create secret generic ${SECRET_NAME} \
--namespace ${NAMESPACE} \
--from-file=vault.key=${TMPDIR}/vault.key \
--from-file=vault.crt=${TMPDIR}/vault.crt \
--from-file=vault.ca=${TMPDIR}/vault.ca
```shell-session
$ kubectl create secret generic ${SECRET_NAME} \
--namespace ${NAMESPACE} \
--from-file=vault.key=${TMPDIR}/vault.key \
--from-file=vault.crt=${TMPDIR}/vault.crt \
--from-file=vault.ca=${TMPDIR}/vault.ca
# secret/vault-server-tls created
```
## 3. Helm Configuration
Expand Down

0 comments on commit 211f4d2

Please sign in to comment.