scripts.go 7.54 KB
Newer Older
lukas.burgey's avatar
Add v3    
lukas.burgey committed
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package scripts

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
)

type (
	// Credential is currently a ssh key, but may be a password hash in the future
	Credential struct {
		ID    int    `json:"id,omitempty"`
		Type  string `json:"type,omitempty"`
		Name  string `json:"name"`
		Value string `json:"value"`
	}

	// UserCredentialStates serves to inform the backend about the per credential states
	// after the script run
	// This maps a credential type like "ssh_key" to a map of the states of credentials of this
	// type.
	UserCredentialStates map[string]map[string]State

	// UserCredentials maps a credential type to the credentials of this type
	UserCredentials map[string][]Credential

	// UserInfo info about the user
	UserInfo map[string]interface{}

	// User contains information concerning the user of a deployment
	User struct {
		UserInfo    UserInfo        `json:"userinfo"`
		Credentials UserCredentials `json:"credentials"`
	}

	// Input of the deployment script
	Input struct {
		// StateTarget is the state which is to be reached by this deployment task
		// StateTarget is either Deployed or NotDeployed
		StateTarget State `json:"state_target"`

		// User describes the user of this deployment task
		User User `json:"user"`

		// Answers is an answered questionnaire relating to an Output.Questionnaire
		//
		// Maps question names (corresponding to the Output.Questionnaire) to the answers of the user.
		// The type of an answer must be string, if there is no mapping for the key in the corresponding Output.QuestionnaireAnswers.
		// If there is such a mapping the answer type must be of the type indicated by the mapping. See Output.QuestionnaireAnswers for details.
		Answers map[string]interface{} `json:"answers,omitempty"`
	}

	// Output of the deployment script
	Output struct {
		// State describes the state of the deployment, after the script execution.
		// When State == Questionnaire then Output.Questionnaire *must* be set.
		// When State == Questionnaire then Output.QuestionnaireAnswers *can* be set.
		// When State == Deployed then Output.Credentials *can* be set.
		State State `json:"state"`

		// Msg is a message for the user.
		Msg string `json:"message"`

		// Questionnaire allows the script to requets further information from the user.
		//
		// This field is Ignored when Output.State is not Questionnaire.
		// Questionnaire maps a question name to a question text.
		Questionnaire map[string]string `json:"questionnaire,omitempty"`

		// QuestionnaireAnswers allows the script to control which answers the user can give.
		//
		// This field is Ignored when Output.State is not Questionnaire.
		// QuestionnaireAnswers maps a question name (corresponding to the question names in Output.Questionnaire) to one of the following:
		// 	- a boolean value indicating a yes/no question with the default selection being the value itself
		// 	- a list of strings or integers indicating that the user must select one of the options of the list
		// 	- a string value indicating that the answer must be of type string. The value itself will be set as default answer for the user.
		//
		// Providing a mapping in QuestionnaireAnswers for a question is optional. If no mapping is provided the default answer must be a string.
		QuestionnaireAnswers map[string]interface{} `json:"questionnaire_answers,omitempty"`

		// Credentials are additionnal credentials for the user.
		// Examples are additional passwords.
		// This field is ignored by the client when Output.State is not Deployed.
		// Credentials maps a credential name to a credential value.
		Credentials map[string]string `json:"credentials,omitempty"`

		// UserCredentialStates are the State s of the credentials found in Input.User.Credentials.
		// This field is not mandatory. The client will assume that all credentials have the State
		// Output.State if this field is not given.
		UserCredentialStates UserCredentialStates `json:"user_credential_states,omitempty"`
	}

	// State is a string enum
	// The enum values for State are listed below
	State string
)

const (
	// Deployed value for State
	Deployed State = "deployed"

	// NotDeployed value for State
	NotDeployed State = "not_deployed"

	// Rejected value for State
	// the user can never be deployed
	Rejected State = "rejected"

	// Failed value for State
	// the deployment can be retried
	Failed State = "failed"

	// Questionnaire value for State
	// the user has to fill the questionnaire
	// with the questionnaire data the deployment can be retried
	Questionnaire State = "questionnaire"
)

func (u User) String() string {
	if email, ok := u.UserInfo["email"]; ok {
		return email.(string)
	}
	if name, ok := u.UserInfo["name"]; ok {
		return name.(string)
	}
	return ""
}

func (i Input) String() string {
	iBytes, err := i.Marshal()
	if err != nil {
		log.Fatal(err)
	}
	return string(iBytes)
}

func (o Output) String() string {
	oBytes, err := o.Marshal()
	if err != nil {
		log.Fatal(err)
	}
	return string(oBytes)
}

// Marshal encodes an Input as json
func (i Input) Marshal() (iBytes []byte, err error) {
	iBytes, err = json.MarshalIndent(i, "", "    ")
	return
}

// Marshal encodes an Output as json
func (o Output) Marshal() (oBytes []byte, err error) {
	oBytes, err = json.MarshalIndent(o, "", "    ")
	return
}

// UnmarshalInput decodes a json encoded input and does some minor sanity checking
func UnmarshalInput(inputBytes []byte, i *Input) (err error) {
	// check if state_target exists
	if bytes.Index(inputBytes, []byte(`"state_target"`)) < 0 {
		err = fmt.Errorf("Output does not contain the field 'state'")
		return
	}

	err = json.Unmarshal(inputBytes, i)
	return
}

// UnmarshalOutput decodes a json encoded output and does some minor sanity checking
func UnmarshalOutput(inputBytes []byte, o *Output) (err error) {
	if o == nil {
		err = fmt.Errorf("Output pointer is nil")
		return
	}

	// check if state exists
	if bytes.Index(inputBytes, []byte(`"state"`)) < 0 {
		err = fmt.Errorf("Output does not contain the field 'state'")
		return
	}

	// unmarshal json
	err = json.Unmarshal(inputBytes, o)
	if err != nil {
		return
	}

	// check questionnaire sanity
	for qName, answer := range o.QuestionnaireAnswers {
		if _, ok := o.Questionnaire[qName]; !ok {
			err = fmt.Errorf("QuestionnaireAnswers contains key '%s' which is missing in Questionnaire", qName)
			return
		}

		// check answer type
		switch t := answer.(type) {
		case string:
			break
		case int:
			break
		case bool:
			break
		case []string:
			break
		case []int:
			break
		default:
			err = fmt.Errorf("QuestionnaireAnswers contains invalid answer type: %v (%v)", t, answer)

		}
	}

	return
}

// SanityCheck checks if o is a sane Output of a script when i s the corresponding input
func SanityCheck(i *Input, o *Output) error {
	// check o.UserCredentialStates against i.User.UserCredentials
	if len(o.UserCredentialStates) > 0 {
		// check each credential type
		for oCredentialType, oCredentialMap := range o.UserCredentialStates {
			iCredentials, ok := i.User.Credentials[oCredentialType]
			if !ok {
				return fmt.Errorf("Credential type %s did not exist in the Input", oCredentialType)
			}

			// in Input credentials are stored in a list, but in the Output as a dictionary :/

			// check if the credentials exist in the input
			for oCredentialName := range oCredentialMap {
				found := false
				for _, iCredential := range iCredentials {
					if iCredential.Name == oCredentialName {
						found = true
						break
					}
				}
				if !found {
					return fmt.Errorf("Credential %s of type %s did not exist in the Input", oCredentialName, oCredentialType)
				}
			}

		}
	}

	// TODO expand this check
	return nil
}