mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-14 18:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
156 lines
4.4 KiB
Go
156 lines
4.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package pki
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/vault/helper/constants"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAcmeConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cluster, client, _ := setupAcmeBackend(t)
|
|
defer cluster.Cleanup()
|
|
|
|
cases := []struct {
|
|
name string
|
|
AcmeConfig map[string]interface{}
|
|
prefixUrl string
|
|
validConfig bool
|
|
works bool
|
|
}{
|
|
{"unspecified-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", true, true},
|
|
{"bad-policy-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"default_directory_policy": "bad",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", false, false},
|
|
{"forbid-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"default_directory_policy": "forbid",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", true, false},
|
|
{"sign-verbatim-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"default_directory_policy": "sign-verbatim",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", true, true},
|
|
{"role-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"default_directory_policy": "role:exists",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", true, true},
|
|
{"bad-role-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "*",
|
|
"default_directory_policy": "role:notgood",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", false, true},
|
|
{"disallowed-role-root", map[string]interface{}{
|
|
"enabled": true,
|
|
"allowed_issuers": "*",
|
|
"allowed_roles": "good",
|
|
"default_directory_policy": "role:exists",
|
|
"dns_resolver": "",
|
|
"eab_policy_name": "",
|
|
}, "acme/", false, false},
|
|
}
|
|
|
|
roleConfig := map[string]interface{}{
|
|
"issuer_ref": "default",
|
|
"allowed_domains": "example.com",
|
|
"allow_subdomains": true,
|
|
"max_ttl": "720h",
|
|
}
|
|
|
|
testCtx := context.Background()
|
|
|
|
for _, tc := range cases {
|
|
deadline := time.Now().Add(1 * time.Minute)
|
|
subTestCtx, _ := context.WithDeadline(testCtx, deadline)
|
|
|
|
_, err := client.Logical().WriteWithContext(subTestCtx, "pki/roles/exists", roleConfig)
|
|
require.NoError(t, err)
|
|
_, err = client.Logical().WriteWithContext(subTestCtx, "pki/roles/good", roleConfig)
|
|
require.NoError(t, err)
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := client.Logical().WriteWithContext(subTestCtx, "pki/config/acme", tc.AcmeConfig)
|
|
|
|
if tc.validConfig {
|
|
require.NoError(t, err)
|
|
} else {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
|
|
_, err = client.Logical().ReadWithContext(subTestCtx, "pki/acme/directory")
|
|
if tc.works {
|
|
require.NoError(t, err)
|
|
|
|
baseAcmeURL := "/v1/pki/" + tc.prefixUrl
|
|
accountKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
require.NoError(t, err, "failed creating rsa key")
|
|
|
|
acmeClient := getAcmeClientForCluster(t, cluster, baseAcmeURL, accountKey)
|
|
|
|
// Create new account
|
|
_, err = acmeClient.Discover(subTestCtx)
|
|
require.NoError(t, err, "failed acme discovery call")
|
|
} else {
|
|
require.Error(t, err, "Acme Configuration should prevent usage")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAcmeExternalPolicyOss make sure setting external-policy on OSS within acme configuration fails
|
|
func TestAcmeExternalPolicyOss(t *testing.T) {
|
|
if constants.IsEnterprise {
|
|
t.Skip("this test is only valid on OSS")
|
|
}
|
|
|
|
t.Parallel()
|
|
b, s := CreateBackendWithStorage(t)
|
|
|
|
values := []string{"external-policy", "external-policy:", "external-policy:test"}
|
|
for _, value := range values {
|
|
t.Run(value, func(st *testing.T) {
|
|
_, err := CBWrite(b, s, "config/acme", map[string]interface{}{
|
|
"enabled": true,
|
|
"default_directory_policy": value,
|
|
})
|
|
|
|
require.Error(st, err, "should have failed setting acme config")
|
|
})
|
|
}
|
|
}
|