main.go 4.83 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
6
package main

import (
	"io/ioutil"
	"log"
	"net/http"
Lukas Burgey's avatar
Lukas Burgey committed
7
	"os"
Lukas Burgey's avatar
Lukas Burgey committed
8

Lukas Burgey's avatar
Lukas Burgey committed
9
	"git.scc.kit.edu/feudal/feudalClient/config"
Lukas Burgey's avatar
Lukas Burgey committed
10
	deps "git.scc.kit.edu/feudal/feudalClient/deployments"
Lukas Burgey's avatar
Lukas Burgey committed
11
12
13
14
15
	"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
16
	kingpin "gopkg.in/alecthomas/kingpin.v2"
Lukas Burgey's avatar
Lukas Burgey committed
17
18
19
)

var (
Lukas Burgey's avatar
Lukas Burgey committed
20
	app = kingpin.New(
Lukas Burgey's avatar
Lukas Burgey committed
21
22
23
24
		"FEUDAL Client",
		"Client for the Federated User Credential Deployment Portal (FEUDAL)",
	).Author(
		"Lukas Burgey",
Lukas Burgey's avatar
Lukas Burgey committed
25
	).Version(
Lukas Burgey's avatar
Lukas Burgey committed
26
		"v2.1.1",
Lukas Burgey's avatar
Lukas Burgey committed
27
	)
Lukas Burgey's avatar
Lukas Burgey committed
28
29
30
31
32
33
	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 a JSON schema (Draft 4, see json-schema.org) for the validation of script inputs, outputs and the config of this client.")
	printSelector    = cmdSchema.Arg("selector", "Select the schema to generate. Options: input, output, config").Required().Enum("input", "output", "config")
	cmdValidate      = app.Command("validate", "Validate a input/output/config json. The json needs to be passed via stdin.")
	validateSelector = cmdValidate.Arg("selector", "Select the schema to validate against. Options: input, output, config").Required().Enum("input", "output", "config")
Lukas Burgey's avatar
Lukas Burgey committed
34

Lukas Burgey's avatar
Lukas Burgey committed
35
	configFile          = app.Flag("config", "Config file to use.").Short('c').String()
Lukas Burgey's avatar
Lukas Burgey committed
36
37
38
39
	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
40
41
)

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

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

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

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

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

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

Lukas Burgey's avatar
Lukas Burgey committed
85
	var err error
Lukas Burgey's avatar
Lukas Burgey committed
86
87
88
89
90
91
92
93
94
95
96
97
98
	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
99
	}
Lukas Burgey's avatar
Lukas Burgey committed
100

Lukas Burgey's avatar
Lukas Burgey committed
101
102
103
104
105
106
107
108
109
110
	// 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
111
}
Lukas Burgey's avatar
Lukas Burgey committed
112

Lukas Burgey's avatar
Lukas Burgey committed
113
func prepareDebugging(conf *config.Config) {
Lukas Burgey's avatar
Lukas Burgey committed
114
115

	// Override the debug configuration with parameters
Lukas Burgey's avatar
Lukas Burgey committed
116
117
118
119
120
	if *debugAll {
		*scriptDebugging = true
		*backendDebugging = true
	}
	if *scriptDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
121
		conf.Debug.Scripts = true
Lukas Burgey's avatar
Lukas Burgey committed
122
123
	}
	if *backendDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
124
		conf.Debug.Backend = true
Lukas Burgey's avatar
Lukas Burgey committed
125
126
	}
	if *sequentialExecution {
Lukas Burgey's avatar
Lukas Burgey committed
127
		conf.Debug.Sequential = true
Lukas Burgey's avatar
Lukas Burgey committed
128
129
	}

Lukas Burgey's avatar
Lukas Burgey committed
130
131
132
133
134
135
136
137
	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
138
	}
Lukas Burgey's avatar
Lukas Burgey committed
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
}

func main() {
	var err error

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

	// Execute commands that don't need the config
	switch cmd {
	case "schema":
		printSchema(*printSelector)
		return
	case "validate":
		result, err := validateJSON(*validateSelector, os.Stdin)
		if err != nil {
			log.Fatal(err)
		}
		printValidationResult(result)
		return
	}

	// read the config file
	conf, err := config.ReadConfig(*configFile)
	if err != nil {
		log.Fatalf("[Conf] %s", err)
	}

	prepareDebugging(conf)
Lukas Burgey's avatar
Lukas Burgey committed
168
169
170

	switch cmd {
	case "start":
Lukas Burgey's avatar
Lukas Burgey committed
171
		// sync the config with the backend
Lukas Burgey's avatar
Lukas Burgey committed
172
		if err := conf.Sync(); err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
173
			log.Printf("[Conf] Error synchronizing configuration: %s", err)
Lukas Burgey's avatar
Lukas Burgey committed
174
175
			return
		}
Lukas Burgey's avatar
Lukas Burgey committed
176
		start(conf)
Lukas Burgey's avatar
Lukas Burgey committed
177
178

	case "deregister":
Lukas Burgey's avatar
Lukas Burgey committed
179
		deregister(conf)
Lukas Burgey's avatar
Lukas Burgey committed
180
181
	}
}