summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/cert_test.go
blob: 96b626b5c98093a68b0d6412ab2590384740723a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// Author: mpolden

package cmd

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/vespa-engine/vespa/client/go/vespa"
)

func TestCert(t *testing.T) {
	homeDir := filepath.Join(t.TempDir(), ".vespa")
	pkgDir := mockApplicationPackage(t, false)
	out, _ := execute(command{args: []string{"cert", "-a", "t1.a1.i1", pkgDir}, homeDir: homeDir}, t, nil)

	app, err := vespa.ApplicationFromString("t1.a1.i1")
	assert.Nil(t, err)

	appDir := filepath.Join(pkgDir, "src", "main", "application")
	pkgCertificate := filepath.Join(appDir, "security", "clients.pem")
	certificate := filepath.Join(homeDir, app.String(), "data-plane-public-cert.pem")
	privateKey := filepath.Join(homeDir, app.String(), "data-plane-private-key.pem")

	assert.Equal(t, fmt.Sprintf("Success: Certificate written to %s\nSuccess: Certificate written to %s\nSuccess: Private key written to %s\n", pkgCertificate, certificate, privateKey), out)

	_, outErr := execute(command{args: []string{"cert", "-a", "t1.a1.i1", pkgDir}, homeDir: homeDir}, t, nil)
	assert.Contains(t, outErr, fmt.Sprintf("Error: Application package %s already contains a certificate", appDir))
}

func TestCertCompressedPackage(t *testing.T) {
	homeDir := filepath.Join(t.TempDir(), ".vespa")
	pkgDir := mockApplicationPackage(t, true)
	zipFile := filepath.Join(pkgDir, "target", "application.zip")
	err := os.MkdirAll(filepath.Dir(zipFile), 0755)
	assert.Nil(t, err)
	_, err = os.Create(zipFile)
	assert.Nil(t, err)

	_, outErr := execute(command{args: []string{"cert", "-a", "t1.a1.i1", pkgDir}, homeDir: homeDir}, t, nil)
	assert.Contains(t, outErr, "Error: Cannot add certificate to compressed application package")

	err = os.Remove(zipFile)
	assert.Nil(t, err)

	out, _ := execute(command{args: []string{"cert", "-f", "-a", "t1.a1.i1", pkgDir}, homeDir: homeDir}, t, nil)
	assert.Contains(t, out, "Success: Certificate written to")
	assert.Contains(t, out, "Success: Private key written to")
}

func mockApplicationPackage(t *testing.T, java bool) string {
	dir := t.TempDir()
	appDir := filepath.Join(dir, "src", "main", "application")
	if err := os.MkdirAll(appDir, 0755); err != nil {
		t.Fatal(err)
	}
	servicesXML := filepath.Join(appDir, "services.xml")
	if _, err := os.Create(servicesXML); err != nil {
		t.Fatal(err)
	}
	if java {
		if _, err := os.Create(filepath.Join(dir, "pom.xml")); err != nil {
			t.Fatal(err)
		}
	}
	return dir
}