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

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

Lukas Burgey's avatar
Lukas Burgey committed
14
	"gopkg.in/alecthomas/kingpin.v2"
Lukas Burgey's avatar
Lukas Burgey committed
15
16
)

Lukas Burgey's avatar
Lukas Burgey committed
17
18
type (
	rabbitMQConfig struct {
Lukas Burgey's avatar
Lukas Burgey committed
19
20
		Exchanges []string `json:"exchanges"`
		Vhost     string   `json:"vhost"`
Lukas Burgey's avatar
Lukas Burgey committed
21
22
23
24
	}

	fetchedConfig struct {
		RabbitMQConfig rabbitMQConfig `json:"rabbitmq_config"`
Lukas Burgey's avatar
Lukas Burgey committed
25
		Site           string         `json:"site"`
Lukas Burgey's avatar
Lukas Burgey committed
26
27
28
	}

	config struct {
Lukas Burgey's avatar
Lukas Burgey committed
29
30
31
32
33
34
		Host     string `json:"host"`
		Username string `json:"username"`
		Password string `json:"password"`

		// GroupToServices maps a group name to services provided for this group
		// this is for  deployment per _group_
Lukas Burgey's avatar
Lukas Burgey committed
35
		GroupToServices map[string]([]service) `json:"group_to_services"`
Lukas Burgey's avatar
Lukas Burgey committed
36

37
38
39
40
41
42
43
44
45
		// EntitlementToServices maps a entitlement to services provided for users with this
		// entitlement
		EntitlementToServices map[string]([]service) `json:"entitlement_to_services"`

		// FetchIntervalString gets parsed by time.ParseDuration
		FetchIntervalString string `json:"fetch_interval"`
		// ReconnectTimeout gets parsed by time.ParseDuration
		ReconnectTimeoutString string `json:"reconnect_timeout"`

46
47
48
49
50
51
		NewTasks         chan task
		DoneTasks        chan taskReply
		FetchInterval    time.Duration
		ReconnectTimeout time.Duration
		RabbitMQConfig   rabbitMQConfig
		Site             string
Lukas Burgey's avatar
Lukas Burgey committed
52
	}
53
54
55

	// strippedConfig is sent to the backend on startup
	strippedConfig struct {
56
		GroupToServices       map[string]([]service) `json:"group_to_services"`
57
		EntitlementToServices map[string]([]service) `json:"entitlement_to_services"`
58
	}
Lukas Burgey's avatar
Lukas Burgey committed
59
60
)

61
62
63
64
65
const (
	defaultFetchInterval    = 30 * time.Minute
	defaultReconnectTimeout = 10 * time.Second
)

Lukas Burgey's avatar
Lukas Burgey committed
66
var (
Lukas Burgey's avatar
Lukas Burgey committed
67
	client = &http.Client{}
Lukas Burgey's avatar
Lukas Burgey committed
68
69
70
71
72
73
	app    = kingpin.New(
		"FEUDAL Client",
		"Client for the Federated User Credential Deployment Portal (FEUDAL)",
	).Author(
		"Lukas Burgey",
	).Version(
74
		"0.4.0",
Lukas Burgey's avatar
Lukas Burgey committed
75
	)
Lukas Burgey's avatar
Lukas Burgey committed
76
77
78
79
80
	configFile          = app.Arg("config", "Config file to file to use.").Required().String()
	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
81
82
)

83
func logError(err error, msg string) {
Lukas Burgey's avatar
Lukas Burgey committed
84
	if err != nil {
85
		log.Printf("[E] %s: %s", msg, err)
Lukas Burgey's avatar
Lukas Burgey committed
86
87
88
	}
}

89
func (c *config) syncConfig() (err error) {
Lukas Burgey's avatar
Lukas Burgey committed
90
91
92
93
94
95
96
97
98
99

	log.Printf("[Conf] Synchronising configuration with %v", c.Host)

	var (
		strippedConfigBytes []byte
		req                 *http.Request
		resp                *http.Response
		fetchedConfig       fetchedConfig
	)

100
	// we inform the backend which services we provide
Lukas Burgey's avatar
Lukas Burgey committed
101
	strippedConfigBytes, err = json.Marshal(strippedConfig{
102
		GroupToServices:       c.GroupToServices,
103
		EntitlementToServices: c.EntitlementToServices,
104
105
106
107
108
109
	})
	if err != nil {
		return
	}

	// update the services tracked by the backend
Lukas Burgey's avatar
Lukas Burgey committed
110
	req, err = http.NewRequest(
111
112
113
114
		"PUT",
		"https://"+c.Host+"/backend/clientapi/config",
		bytes.NewReader(strippedConfigBytes),
	)
Lukas Burgey's avatar
Lukas Burgey committed
115
116
117
118
	if err != nil {
		return
	}

119
	req.SetBasicAuth(c.Username, c.Password)
120
	req.Header.Set("Content-Type", "application/json")
Lukas Burgey's avatar
Lukas Burgey committed
121
	resp, err = client.Do(req)
Lukas Burgey's avatar
Lukas Burgey committed
122
123
124
125
126
127
128
129
130
	if err != nil {
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
131
	if resp.StatusCode != 200 {
Lukas Burgey's avatar
Lukas Burgey committed
132
		err = fmt.Errorf("Unable to sync configuration (response: %v)", resp.Status)
133
134
135
		return
	}

136
	err = json.Unmarshal(body, &fetchedConfig)
Lukas Burgey's avatar
Lukas Burgey committed
137
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
138
		err = fmt.Errorf("Unable to parse remote configuration: %s %s", err, body)
Lukas Burgey's avatar
Lukas Burgey committed
139
140
		return
	}
141

Lukas Burgey's avatar
Lukas Burgey committed
142
143
	c.RabbitMQConfig = fetchedConfig.RabbitMQConfig
	c.Site = fetchedConfig.Site
144

Lukas Burgey's avatar
Lukas Burgey committed
145
146
	log.Printf("[Conf] Synchronised configuration with %v", c.Host)

Lukas Burgey's avatar
Lukas Burgey committed
147
148
149
	return
}

150
func getConfig(configFile string) (c config, err error) {
151

Lukas Burgey's avatar
Lukas Burgey committed
152
	c.Log("Reading config file %s", configFile)
153

Lukas Burgey's avatar
Lukas Burgey committed
154
	bs, err := ioutil.ReadFile(configFile)
155
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
156
		c.Log("Error reading config file: %s", err)
157
158
159
		return
	}

Lukas Burgey's avatar
Lukas Burgey committed
160
161
	err = json.Unmarshal(bs, &c)
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
162
		c.Log("Error parsing config file: %s", err)
163
164
		return
	}
Lukas Burgey's avatar
Lukas Burgey committed
165

166
167
168
169
170
171
172
173
174
175
176
	// check the config values
	if c.Host == "" {
		log.Fatalf("[Conf] No 'host' in config")
	}
	if c.Username == "" {
		log.Fatalf("[Conf] No 'user' in config")
	}
	if c.Password == "" {
		log.Fatalf("[Conf] No 'password' in config")
	}

Lukas Burgey's avatar
Lukas Burgey committed
177
178
179
180
	var parseError error
	c.FetchInterval, parseError = time.ParseDuration(c.FetchIntervalString)
	if parseError != nil {
		log.Printf("[Conf] Error parsing 'fetch_interval': %s", parseError)
181
			c.FetchInterval = defaultFetchInterval
Lukas Burgey's avatar
Lukas Burgey committed
182
		log.Printf("[Conf] Using default 'fetch_interval' of %v", c.FetchInterval)
183
	}
Lukas Burgey's avatar
Lukas Burgey committed
184
185
186
	c.ReconnectTimeout, parseError = time.ParseDuration(c.ReconnectTimeoutString)
	if parseError != nil {
		log.Printf("[Conf] Error parsing 'reconnect_timeout': %s", parseError)
187
		c.ReconnectTimeout = defaultReconnectTimeout
Lukas Burgey's avatar
Lukas Burgey committed
188
		log.Printf("[Conf] Using default 'reconnect_timeout' of %v", c.ReconnectTimeout)
189
	}
190
191

	// fetch the remote configuration
192
	err = c.syncConfig()
193
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
194
		log.Printf("[Conf] Error fetching remote config: %s", err)
195
196
		return
	}
Lukas Burgey's avatar
Lukas Burgey committed
197

198
199
200
201
202
203
204
205
206
207
	// strip the group authority from entitlement names
	nameExtractor := regexp.MustCompile("^(.*?)#")
	for entName, entServices := range c.EntitlementToServices {
		match := nameExtractor.FindStringSubmatch(entName)
		if len(match) == 2 {
			delete(c.EntitlementToServices, entName)
			c.EntitlementToServices[match[1]] = entServices
		}
	}

208
	c.Log("Groups:       %s", c.GroupToServices)
209
	c.Log("Entitlements: %s", c.EntitlementToServices)
210

Lukas Burgey's avatar
Lukas Burgey committed
211
212
	// initialize the task queues
	c.NewTasks = make(chan task)
213
	c.DoneTasks = make(chan taskReply)
Lukas Burgey's avatar
Lukas Burgey committed
214

Lukas Burgey's avatar
Lukas Burgey committed
215
216
	return
}
Lukas Burgey's avatar
Lukas Burgey committed
217
218

func main() {
Lukas Burgey's avatar
Lukas Burgey committed
219
	var err error
Lukas Burgey's avatar
Lukas Burgey committed
220

Lukas Burgey's avatar
Lukas Burgey committed
221
	// get arguments
Lukas Burgey's avatar
Lukas Burgey committed
222
223
	kingpin.MustParse(app.Parse(os.Args[1:]))

Lukas Burgey's avatar
Lukas Burgey committed
224
225
226
227
228
229
230
231
232
233
	if *debugAll {
		*scriptDebugging = true
		*backendDebugging = true
	}
	if *scriptDebugging {
		log.Printf("[Debug] script debugging enabled")
	}
	if *backendDebugging {
		log.Printf("[Debug] backend debugging enabled")
	}
Lukas Burgey's avatar
Lukas Burgey committed
234
235
236
	if *sequentialExecution {
		log.Printf("[Debug] Executing tasks sequentially")
	}
Lukas Burgey's avatar
Lukas Burgey committed
237

Lukas Burgey's avatar
Lukas Burgey committed
238
	// read the config file
239
	c, err := getConfig(*configFile)
240
	if err != nil {
Lukas Burgey's avatar
Lukas Burgey committed
241
		log.Fatalf("[Exit] No valid config. Exiting")
242
243
	}

244
	if len(c.EntitlementToServices) == 0 && len(c.GroupToServices) == 0 {
Lukas Burgey's avatar
Lukas Burgey committed
245
246
		log.Printf("[P] Not starting pubsub because the are no services to subscribe to")
		return
Lukas Burgey's avatar
Lukas Burgey committed
247
248
	}

249
	// start task handler and responder
250
	go c.taskHandler()
251
	go c.taskResponder()
252

Lukas Burgey's avatar
Lukas Burgey committed
253
	consumer := c.consumer()
254
	defer consumer.close()
Lukas Burgey's avatar
Lukas Burgey committed
255

Lukas Burgey's avatar
Lukas Burgey committed
256
	consumer.startConsuming()
Lukas Burgey's avatar
Lukas Burgey committed
257

Lukas Burgey's avatar
Lukas Burgey committed
258
259
260
	// start the fetcher after the consuming starts
	// -> we miss nothing
	go c.taskFetcher()
Lukas Burgey's avatar
Lukas Burgey committed
261

Lukas Burgey's avatar
Lukas Burgey committed
262
263
264
	// run till killed
	forever := make(chan bool)
	<-forever
Lukas Burgey's avatar
Lukas Burgey committed
265
}