|
| 1 | +package sshutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/ecdsa" |
| 5 | + "crypto/elliptic" |
| 6 | + "crypto/rand" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "go.step.sm/crypto/x509util" |
| 13 | +) |
| 14 | + |
| 15 | +func TestRegisterTemplateFunc(t *testing.T) { |
| 16 | + cr := CertificateRequest{Key: mustGeneratePublicKey(t), Type: UserCert.String()} |
| 17 | + data := CreateTemplateData(UserCert, "jane@example.com", []string{"jane"}) |
| 18 | + |
| 19 | + require.NoError(t, RegisterTemplateFunc("testPrincipals", func(data any) (any, error) { |
| 20 | + m, _ := data.(TemplateData) |
| 21 | + return m[PrincipalsKey], nil |
| 22 | + })) |
| 23 | + t.Cleanup(func() { UnregisterTemplateFunc("testPrincipals") }) |
| 24 | + |
| 25 | + var o Options |
| 26 | + require.NoError(t, WithTemplate(`{{ testPrincipals $ | toJson }}`, data)(cr, &o)) |
| 27 | + assert.Equal(t, `["jane"]`, o.CertBuffer.String()) |
| 28 | +} |
| 29 | + |
| 30 | +func TestRegisterTemplateFuncErrors(t *testing.T) { |
| 31 | + require.Error(t, RegisterTemplateFunc("", func() string { return "" })) |
| 32 | + require.Error(t, RegisterTemplateFunc("notfn", "a string")) |
| 33 | + |
| 34 | + err := RegisterTemplateFunc("toJson", func() string { return "" }) |
| 35 | + require.Error(t, err) |
| 36 | + assert.Contains(t, err.Error(), "built in and cannot be replaced") |
| 37 | +} |
| 38 | + |
| 39 | +// TestRegistriesAreSeparate checks that a function registered for one kind of |
| 40 | +// certificate is not available to the other. |
| 41 | +func TestRegistriesAreSeparate(t *testing.T) { |
| 42 | + require.NoError(t, RegisterTemplateFunc("testSSHOnly", func() string { return "ssh" })) |
| 43 | + t.Cleanup(func() { UnregisterTemplateFunc("testSSHOnly") }) |
| 44 | + |
| 45 | + cr := CertificateRequest{Key: mustGeneratePublicKey(t), Type: UserCert.String()} |
| 46 | + data := CreateTemplateData(UserCert, "jane@example.com", []string{"jane"}) |
| 47 | + |
| 48 | + var o Options |
| 49 | + require.NoError(t, WithTemplate(`{{ testSSHOnly }}`, data)(cr, &o)) |
| 50 | + assert.Equal(t, "ssh", o.CertBuffer.String()) |
| 51 | + |
| 52 | + // The same name is undefined for X.509 templates. |
| 53 | + signer, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 54 | + require.NoError(t, err) |
| 55 | + xcr, err := x509util.CreateCertificateRequest("foo", []string{"foo.com"}, signer) |
| 56 | + require.NoError(t, err) |
| 57 | + var xo x509util.Options |
| 58 | + err = x509util.WithTemplate(`{{ testSSHOnly }}`, x509util.TemplateData{})(xcr, &xo) |
| 59 | + require.Error(t, err) |
| 60 | + assert.Contains(t, err.Error(), `function "testSSHOnly" not defined`) |
| 61 | + |
| 62 | + // An application that wants it in both registers with both. |
| 63 | + require.NoError(t, x509util.RegisterTemplateFunc("testSSHOnly", func() string { return "x509" })) |
| 64 | + t.Cleanup(func() { x509util.UnregisterTemplateFunc("testSSHOnly") }) |
| 65 | + |
| 66 | + var xo2 x509util.Options |
| 67 | + require.NoError(t, x509util.WithTemplate(`{{ testSSHOnly }}`, x509util.TemplateData{})(xcr, &xo2)) |
| 68 | + assert.Equal(t, "x509", xo2.CertBuffer.String()) |
| 69 | +} |
0 commit comments