main.go 4.77 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
package main

import (
Lukas Burgey's avatar
Lukas Burgey committed
4
5
	"encoding/json"
	"fmt"
Lukas Burgey's avatar
Lukas Burgey committed
6
7
8
	"io/ioutil"
	"log"
	"net/http"
Lukas Burgey's avatar
Lukas Burgey committed
9
	"os"
Lukas Burgey's avatar
Lukas Burgey committed
10

Lukas Burgey's avatar
Lukas Burgey committed
11
	"git.scc.kit.edu/feudal/feudalClient/config"
Lukas Burgey's avatar
Lukas Burgey committed
12
	deps "git.scc.kit.edu/feudal/feudalClient/deployments"
Lukas Burgey's avatar
Lukas Burgey committed
13
14
15
16
17
	"git.scc.kit.edu/feudal/feudalClient/sink"
	"git.scc.kit.edu/feudal/feudalClient/sink/script"
	"git.scc.kit.edu/feudal/feudalClient/source"
	"git.scc.kit.edu/feudal/feudalClient/source/amqp"
	"git.scc.kit.edu/feudal/feudalClient/source/rest"
Lukas Burgey's avatar
Lukas Burgey committed
18
19
20
	scripts "git.scc.kit.edu/feudal/feudalScripts/v2"
	"github.com/alecthomas/jsonschema"
	kingpin "gopkg.in/alecthomas/kingpin.v2"
Lukas Burgey's avatar
Lukas Burgey committed
21
22
23
)

var (
Lukas Burgey's avatar
Lukas Burgey committed
24
	app = kingpin.New(
Lukas Burgey's avatar
Lukas Burgey committed
25
26
27
28
		"FEUDAL Client",
		"Client for the Federated User Credential Deployment Portal (FEUDAL)",
	).Author(
		"Lukas Burgey",
Lukas Burgey's avatar
Lukas Burgey committed
29
	).Version(
Lukas Burgey's avatar
Lukas Burgey committed
30
		"v2.1.0",
Lukas Burgey's avatar
Lukas Burgey committed
31
	)
Lukas Burgey's avatar
Lukas Burgey committed
32
33
34
35
	cmdStart       = app.Command("start", "Starts the client in its normal operation mode.").Default()
	cmdDeregister  = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
	cmdSchema      = app.Command("schema", "Can be used to generate JSON schemas (Draft 4, see json-schema.org) for the validation of script inputs and outputs.")
	schemaSelector = cmdSchema.Arg("selector", "Select the schema to generate.").Required().Enum("input", "output", "config")
Lukas Burgey's avatar
Lukas Burgey committed
36

Lukas Burgey's avatar
Lukas Burgey committed
37
	configFile          = app.Flag("config", "Config file to use.").Short('c').String()
Lukas Burgey's avatar
Lukas Burgey committed
38
39
40
41
	scriptDebugging     = app.Flag("debug-scripts", "Display debugging info concerning executed scripts").Bool()
	backendDebugging    = app.Flag("debug-backend", "Display debugging info concerning the backend").Bool()
	debugAll            = app.Flag("debug", "Display all debugging info").Bool()
	sequentialExecution = app.Flag("seq", "Execute tasks sequentially").Bool()
Lukas Burgey's avatar
Lukas Burgey committed
42
43
)

Lukas Burgey's avatar
Lukas Burgey committed
44
func deregister(conf *config.Config) {
Lukas Burgey's avatar
Lukas Burgey committed
45
46
47
48
49
50
	var (
		err  error
		req  *http.Request
		resp *http.Response
	)
	log.Printf("Deregistering this client at the backend")
Lukas Burgey's avatar
Lukas Burgey committed
51

Lukas Burgey's avatar
Lukas Burgey committed
52
53
	req, err = http.NewRequest(
		"PUT",
Lukas Burgey's avatar
Lukas Burgey committed
54
		"https://"+conf.Hostname+"/backend/clientapi/deregister",
Lukas Burgey's avatar
Lukas Burgey committed
55
56
57
58
		nil,
	)
	if err != nil {
		log.Fatalf("Unable to deregister: %s", err)
Lukas Burgey's avatar
Lukas Burgey committed
59
	}
Lukas Burgey's avatar
Lukas Burgey committed
60
	req.SetBasicAuth(conf.Username, conf.Password)
Lukas Burgey's avatar
Lukas Burgey committed
61

Lukas Burgey's avatar
Lukas Burgey committed
62
	resp, err = (&http.Client{}).Do(req)
63
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
64
		log.Fatalf("Unable to deregister: %s", err)
65
	}
Lukas Burgey's avatar
Lukas Burgey committed
66
67
68
69
70
71
72
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("Cannot read response body")
	}
	log.Printf("Response: %s", body)
}
73

Lukas Burgey's avatar
Lukas Burgey committed
74
75
func start(conf *config.Config) {
	if len(conf.EntitlementToServiceIDs) == 0 && len(conf.GroupToServiceIDs) == 0 {
Lukas Burgey's avatar
Lukas Burgey committed
76
77
		log.Printf("[P] Not starting pubsub because the are no services to subscribe to")
		return
Lukas Burgey's avatar
Lukas Burgey committed
78
79
	}

Lukas Burgey's avatar
Lukas Burgey committed
80
81
82
	// wire the pipes from sources to sink(s)
	sources := []source.Source{new(rest.Source), new(amqp.Source)}
	srcPipes := make([]<-chan deps.Dep, len(sources))
83

Lukas Burgey's avatar
Lukas Burgey committed
84
85
	sinks := []sink.Sink{new(script.Sink)}
	sinkPipe := make(chan deps.Dep)
Lukas Burgey's avatar
Lukas Burgey committed
86

Lukas Burgey's avatar
Lukas Burgey committed
87
	var err error
Lukas Burgey's avatar
Lukas Burgey committed
88
89
90
91
92
93
94
95
96
97
98
99
100
	for _, sink := range sinks {
		sink.Init(conf)
		err = sink.Connect((<-chan deps.Dep)(sinkPipe))
		if err != nil {
			log.Printf("Error connecting sink: %s", err)
		}
	}
	for i, src := range sources {
		src.Init(conf)
		srcPipes[i], err = src.Connect()
		if err != nil {
			log.Printf("Error connecting source: %s", err)
		}
Lukas Burgey's avatar
Lukas Burgey committed
101
	}
Lukas Burgey's avatar
Lukas Burgey committed
102

Lukas Burgey's avatar
Lukas Burgey committed
103
104
105
106
107
108
109
110
111
112
	// TODO rework
	var received deps.Dep
	for {
		select {
		case received = <-srcPipes[0]:
			sinkPipe <- received
		case received = <-srcPipes[1]:
			sinkPipe <- received
		}
	}
Lukas Burgey's avatar
Lukas Burgey committed
113
}
Lukas Burgey's avatar
Lukas Burgey committed
114

Lukas Burgey's avatar
Lukas Burgey committed
115
116
117
118
119
120
121
122
123
func printSchema(i interface{}) {
	schema := jsonschema.Reflect(i)
	bs, err := json.MarshalIndent(schema, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", bs)
}

Lukas Burgey's avatar
Lukas Burgey committed
124
125
126
127
128
129
func main() {
	var err error

	// get arguments
	cmd := kingpin.MustParse(app.Parse(os.Args[1:]))

Lukas Burgey's avatar
Lukas Burgey committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	// Do things that need no config or debugging
	switch cmd {
	case "schema":
		switch *schemaSelector {
		case "input":
			printSchema(scripts.Input{})
		case "output":
			printSchema(scripts.Output{})
		case "config":
			printSchema(config.Config{})
		}
		return
	}

Lukas Burgey's avatar
Lukas Burgey committed
144
	// read the config file
Lukas Burgey's avatar
Lukas Burgey committed
145
146
147
	if *configFile == "" {
		log.Fatalf("No config file. Unable to proceed. Provide the --config flag")
	}
Lukas Burgey's avatar
Lukas Burgey committed
148
149
150
151
152
153
	conf, err := config.ReadConfig(*configFile)
	if err != nil {
		log.Fatalf("[Conf] %s", err)
	}

	// Override the debug configuration with parameters
Lukas Burgey's avatar
Lukas Burgey committed
154
155
156
157
158
	if *debugAll {
		*scriptDebugging = true
		*backendDebugging = true
	}
	if *scriptDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
159
		conf.Debug.Scripts = true
Lukas Burgey's avatar
Lukas Burgey committed
160
161
	}
	if *backendDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
162
		conf.Debug.Backend = true
Lukas Burgey's avatar
Lukas Burgey committed
163
164
	}
	if *sequentialExecution {
Lukas Burgey's avatar
Lukas Burgey committed
165
		conf.Debug.Sequential = true
Lukas Burgey's avatar
Lukas Burgey committed
166
167
	}

Lukas Burgey's avatar
Lukas Burgey committed
168
169
170
171
172
173
174
175
	if conf.Debug.Scripts {
		log.Printf("[Debug] script debugging enabled")
	}
	if conf.Debug.Backend {
		log.Printf("[Debug] backend debugging enabled")
	}
	if conf.Debug.Sequential {
		log.Printf("[Debug] Executing tasks sequentially")
Lukas Burgey's avatar
Lukas Burgey committed
176
177
178
179
	}

	switch cmd {
	case "start":
Lukas Burgey's avatar
Lukas Burgey committed
180
181
182
		// sync the config with the backend
		if conf.Sync() != nil {
			log.Printf("[Conf] Error synchronizing configuration: %s", err)
Lukas Burgey's avatar
Lukas Burgey committed
183
184
			return
		}
Lukas Burgey's avatar
Lukas Burgey committed
185
		start(conf)
Lukas Burgey's avatar
Lukas Burgey committed
186
187

	case "deregister":
Lukas Burgey's avatar
Lukas Burgey committed
188
		deregister(conf)
Lukas Burgey's avatar
Lukas Burgey committed
189
190
	}
}