summaryrefslogtreecommitdiffstats
path: root/client/go/cmd/cert.go
blob: 8b75ad2a50913901081800563a8c1aafa28ba407 (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa cert command
// Author: mpolden
package cmd

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

	"github.com/spf13/cobra"
	"github.com/vespa-engine/vespa/util"
	"github.com/vespa-engine/vespa/vespa"
)

var overwriteCertificate bool

func init() {
	certCmd.Flags().BoolVarP(&overwriteCertificate, "force", "f", false, "Force overwrite of existing certificate and private key")
	rootCmd.AddCommand(certCmd)
}

var certCmd = &cobra.Command{
	Use:   "cert",
	Short: "Creates a new private key and self-signed certificate",
	Long: `Applications in Vespa Cloud are required to secure their data plane with mutual TLS. ` +
		`This command creates a self-signed certificate suitable for development purposes. ` +
		`See https://cloud.vespa.ai/en/security-model for more information on the Vespa Cloud security model.`,
	Run: func(cmd *cobra.Command, args []string) {
		var path string
		if len(args) > 0 {
			path = args[0]
		} else {
			var err error
			path, err = os.Getwd()
			util.FatalIfErr(err)
		}

		pkg, err := vespa.FindApplicationPackage(path)
		util.FatalIfErr(err)
		if pkg.HasCertificate() {
			util.Print("Certificate already exists")
			return
		}

		// TODO: Consider writing key pair inside ~/.vespa/<app-name>/ instead so that vespa document commands can easily
		// locate key pair
		securityDir := filepath.Join(pkg.Path, "security")
		privateKeyFile := filepath.Join(path, "data-plane-private-key.pem")
		certificateFile := filepath.Join(path, "data-plane-public-cert.pem")
		pkgCertificateFile := filepath.Join(securityDir, "clients.pem")

		keyPair, err := vespa.CreateKeyPair()
		util.FatalIfErr(err)

		err = os.MkdirAll(securityDir, 0755)
		util.FatalIfErr(err)
		err = keyPair.WriteCertificateFile(pkgCertificateFile, overwriteCertificate)
		util.FatalIfErr(err)
		err = keyPair.WriteCertificateFile(certificateFile, overwriteCertificate)
		util.FatalIfErr(err)
		err = keyPair.WritePrivateKeyFile(privateKeyFile, overwriteCertificate)
		util.FatalIfErr(err)

		// TODO: Just use log package, which has Printf
		util.Print(fmt.Sprintf("Certificate written to %s", pkgCertificateFile))
		util.Print(fmt.Sprintf("Certificate written to %s", certificateFile))
		util.Print(fmt.Sprintf("Private key written to %s", privateKeyFile))
	},
}