summaryrefslogtreecommitdiff
path: root/magefiles/step.go
blob: 25cf23b0c89dbb5782895c834cbb3723e6a1447d (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//go:build mage
// +build mage

package main

import (
	"context"
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/magefile/mage/mg"
	"github.com/magefile/mage/sh"
	"github.com/magefile/mage/target"
	"github.com/xlgmokha/x/pkg/env"
	"github.com/xlgmokha/x/pkg/x"
)

type Step mg.Namespace

func (s Step) Clean() error {
	globs := []string{
		"tmp/step/*/*",
	}
	for _, item := range globs {
		fs, err := filepath.Glob(item)
		if err != nil {
			return err
		}
		for _, f := range fs {
			if strings.HasSuffix(f, "/.keep") {
				continue
			}
			if err := os.RemoveAll(f); err != nil {
				return err
			}
		}
	}
	return nil
}

func (s Step) Setup() {
	mg.SerialDeps(s.mkPassword, s.createCA, s.enableACMEProvisioner)
}

func (s Step) Install() error {
	return sh.RunWithV(
		s.env(),
		"step",
		"certificate",
		"install",
		s.pathPlus("/certs/root_ca.crt"),
	)
}

func (s Step) Server(ctx context.Context) error {
	mg.SerialDeps(s.Setup)

	return sh.RunWithV(
		s.env(),
		"step-ca",
		s.pathPlus("config/ca.json"),
		"--password-file="+s.pathPlus("password.txt"),
	)
}

func (s Step) Provisioners() error {
	return sh.RunV("curl", "-k", "-s", "https://localhost:8081/provisioners")
}

func (s Step) ACME() error {
	return sh.RunV("curl", "-k", "-s", "https://localhost:8081/acme/acme/directory")
}

func (s Step) Status() {
	mg.SerialDeps(s.Provisioners, s.ACME)
}

func (s Step) mkPassword() error {
	file := s.passwordFile()
	if ok, err := target.Dir(file); err != nil || !ok {
		return nil
	}

	return os.WriteFile(file, []byte("password"), 0600)
}

func (s Step) createCA() error {
	if ok, err := target.Dir(s.pathPlus("config/ca.json"), s.passwordFile()); err != nil || !ok {
		return nil
	}

	return sh.RunWithV(
		s.env(),
		"step",
		"ca",
		"init",
		"--deployment-type=standalone",
		"--address=localhost:8081",
		"--dns=localhost",
		"--dns=*.localhost",
		"--name=CA",
		"--provisioner=example",
		"--provisioner-password-file="+s.passwordFile(),
		"--password-file="+s.passwordFile(),
	)
}

func (s Step) enableACMEProvisioner() error {
	bytes, err := ioutil.ReadFile(s.pathPlus("config/ca.json"))
	if err != nil {
		return err
	}

	items := map[string]interface{}{}
	if err := json.Unmarshal(bytes, &items); err != nil {
		return err
	}

	provisioners := items["authority"].(map[string]interface{})["provisioners"].([]interface{})
	if len(provisioners) < 2 {
		return sh.RunWithV(s.env(), "step", "ca", "provisioner", "add", "acme", "--type", "ACME")
	}
	return nil
}

func (step Step) passwordFile() string {
	return step.pathPlus("password.txt")
}

func (s Step) path() string {
	return env.Fetch("STEPPATH", filepath.Join(x.Must(os.Getwd()), "/tmp/step"))
}

func (s Step) env() map[string]string {
	return map[string]string{
		"STEPPATH": s.path(),
		"HOST":     "localhost",
		"PORT":     "8081",
	}
}

func (s Step) pathPlus(path string) string {
	return filepath.Join(s.path(), path)
}