11package helpers
22
33import (
4+ "crypto/rand"
5+ "crypto/rsa"
6+ "crypto/x509"
7+ "crypto/x509/pkix"
8+ "encoding/pem"
49 "fmt"
5- "net"
6- "strconv"
7- "testing"
8-
910 "github.com/hashicorp/go-hclog"
1011 kv "github.com/hashicorp/vault-plugin-secrets-kv"
1112 "github.com/hashicorp/vault/api"
1213 credAppRole "github.com/hashicorp/vault/builtin/credential/approle"
1314 credCert "github.com/hashicorp/vault/builtin/credential/cert"
1415 credUserPass "github.com/hashicorp/vault/builtin/credential/userpass"
16+ "github.com/hashicorp/vault/builtin/logical/pki"
1517 "github.com/hashicorp/vault/http"
1618 "github.com/hashicorp/vault/sdk/logical"
1719 "github.com/hashicorp/vault/vault"
20+ "math/big"
21+ "net"
22+ "strconv"
23+ "testing"
24+ "time"
1825)
1926
2027// Test Constants
@@ -302,7 +309,8 @@ func CreateTestCertificateVault(t *testing.T) (*vault.TestCluster, string, strin
302309
303310 coreConfig := & vault.CoreConfig {
304311 LogicalBackends : map [string ]logical.Factory {
305- "kv" : kv .Factory ,
312+ "kv" : kv .Factory ,
313+ "pki" : pki .Factory ,
306314 },
307315 CredentialBackends : map [string ]logical.Factory {
308316 "cert" : credCert .Factory ,
@@ -333,8 +341,17 @@ func CreateTestCertificateVault(t *testing.T) (*vault.TestCluster, string, strin
333341 t .Fatal (err )
334342 }
335343
344+ write , err := client .Logical ().Write ("auth/cert/certs/vault-cert" , map [string ]interface {}{
345+ "display_name" : "vault-cert" ,
346+ "policies" : "cert-kv,cert-secret" ,
347+ "certificate" : string (cluster .CACertPEM ),
348+ })
349+ if err != nil && write == nil {
350+ return nil , "" , ""
351+ }
352+
336353 // Create Policy for secret/foo
337- err : = client .Sys ().PutPolicy ("cert-secret" , "path \" secret/*\" { capabilities = [\" read\" ,\" list\" ] }" )
354+ err = client .Sys ().PutPolicy ("cert-secret" , "path \" secret/*\" { capabilities = [\" read\" ,\" list\" ] }" )
338355 if err != nil {
339356 t .Fatal (err )
340357 }
@@ -345,15 +362,6 @@ func CreateTestCertificateVault(t *testing.T) (*vault.TestCluster, string, strin
345362 t .Fatal (err )
346363 }
347364
348- _ , err = client .Logical ().Write ("auth/approle/role/role1" , map [string ]interface {}{
349- "bind_secret_id" : "true" ,
350- "period" : "300" ,
351- "policies" : "cert-secret, cert-kv" ,
352- })
353- if err != nil {
354- t .Fatal (err )
355- }
356-
357365 _ , err = client .Logical ().Write ("secret/testing" , map [string ]interface {}{
358366 "name" : "test-name" ,
359367 "namespace" : "test-namespace" ,
@@ -463,19 +471,49 @@ func CreateTestCertificateVault(t *testing.T) (*vault.TestCluster, string, strin
463471 t .Fatal (err )
464472 }
465473
466- secret , err := client . Logical (). Write ( "auth/approle/role/role1/secret-id" , nil )
474+ key , err := rsa . GenerateKey ( rand . Reader , 2048 )
467475 if err != nil {
468476 t .Fatal (err )
469477 }
470- secretID := secret .Data ["secret_id" ].(string )
478+ privateKeyPEM := pem .EncodeToMemory (& pem.Block {
479+ Type : "RSA PRIVATE KEY" ,
480+ Bytes : x509 .MarshalPKCS1PrivateKey (key ),
481+ })
471482
472- secret , err = client .Logical ().Read ("auth/approle/role/role1/role-id" )
483+ csrTemplate := x509.CertificateRequest {
484+ Subject : pkix.Name {
485+ CommonName : "vault-cert" ,
486+ Organization : []string {"Client Org" },
487+ },
488+ }
489+ csrBytes , err := x509 .CreateCertificateRequest (rand .Reader , & csrTemplate , key )
473490 if err != nil {
474491 t .Fatal (err )
475492 }
476- roleID := secret .Data ["role_id" ].(string )
477493
478- return cluster , roleID , secretID
494+ csr , err := x509 .ParseCertificateRequest (csrBytes )
495+ if err != nil {
496+ t .Fatal (err )
497+ }
498+
499+ clientCertTemplate := x509.Certificate {
500+ SerialNumber : big .NewInt (1234567890 ), // Eine eindeutige Seriennummer
501+ Subject : csr .Subject ,
502+ NotBefore : time .Now (),
503+ NotAfter : time .Now ().Add (365 * 24 * time .Hour ), // 1 Jahr Gültigkeit
504+ KeyUsage : x509 .KeyUsageDigitalSignature ,
505+ ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageClientAuth },
506+ }
507+ clientCertBytes , err := x509 .CreateCertificate (rand .Reader , & clientCertTemplate , cluster .CACert , csr .PublicKey , cluster .CAKey )
508+ if err != nil {
509+ t .Fatal (err )
510+ }
511+
512+ clientCertPEM := pem .EncodeToMemory (& pem.Block {
513+ Type : "CERTIFICATE" ,
514+ Bytes : clientCertBytes ,
515+ })
516+ return cluster , string (clientCertPEM ), string (privateKeyPEM )
479517}
480518
481519// CreateTestGithubVault initializes a new test vault with AppRole and Kv v2
0 commit comments