main.go 3.86 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
	"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
	"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.0.1",
Lukas Burgey's avatar
Lukas Burgey committed
27
	)
Lukas Burgey's avatar
Lukas Burgey committed
28
29
30
31
	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.")

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

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

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

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

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

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

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

Lukas Burgey's avatar
Lukas Burgey committed
81
	var err error
Lukas Burgey's avatar
Lukas Burgey committed
82
83
84
85
86
87
88
89
90
91
92
93
94
	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
95
	}
Lukas Burgey's avatar
Lukas Burgey committed
96

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

func main() {
	var err error

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

Lukas Burgey's avatar
Lukas Burgey committed
115
116
117
118
119
120
121
	// read the config file
	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
122
123
124
125
126
	if *debugAll {
		*scriptDebugging = true
		*backendDebugging = true
	}
	if *scriptDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
127
		conf.Debug.Scripts = true
Lukas Burgey's avatar
Lukas Burgey committed
128
129
	}
	if *backendDebugging {
Lukas Burgey's avatar
Lukas Burgey committed
130
		conf.Debug.Backend = true
Lukas Burgey's avatar
Lukas Burgey committed
131
132
	}
	if *sequentialExecution {
Lukas Burgey's avatar
Lukas Burgey committed
133
		conf.Debug.Sequential = true
Lukas Burgey's avatar
Lukas Burgey committed
134
135
	}

Lukas Burgey's avatar
Lukas Burgey committed
136
137
138
139
140
141
142
143
	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
144
145
146
147
	}

	switch cmd {
	case "start":
Lukas Burgey's avatar
Lukas Burgey committed
148
149
150
		// 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
151
152
			return
		}
Lukas Burgey's avatar
Lukas Burgey committed
153
		start(conf)
Lukas Burgey's avatar
Lukas Burgey committed
154
155

	case "deregister":
Lukas Burgey's avatar
Lukas Burgey committed
156
		deregister(conf)
Lukas Burgey's avatar
Lukas Burgey committed
157
158
	}
}