This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
106 lines (87 loc) · 2.86 KB
/
main.go
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
// Copyright 2014-2016 Fraunhofer Institute for Applied Information Technology FIT
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
_ "github.com/linksmart/go-sec/auth/keycloak/obtainer"
"github.com/linksmart/go-sec/auth/obtainer"
"github.com/linksmart/service-catalog/v2/catalog"
"github.com/linksmart/service-catalog/v2/client"
uuid "github.com/satori/go.uuid"
)
var (
confPath = flag.String("conf", "", "Path to the service configuration file")
endpoint = flag.String("endpoint", "", "Service Catalog endpoint")
//discover = flag.Bool("discover", false, "Use DNS-SD service discovery to find Service Catalog endpoint")
// Authentication configuration
authProvider = flag.String("authProvider", "", "Authentication provider name")
authProviderURL = flag.String("authProviderURL", "", "Authentication provider url")
authUser = flag.String("authUser", "", "Auth. server username")
authPass = flag.String("authPass", "", "Auth. server password")
serviceID = flag.String("serviceID", "", "Service ID at the auth. server")
)
const LINKSMART = `
╦ ╦ ╔╗╔ ╦╔═ ╔═╗ ╔╦╗ ╔═╗ ╦═╗ ╔╦╗ R
║ ║ ║║║ ╠╩╗ ╚═╗ ║║║ ╠═╣ ╠╦╝ ║
╩═╝ ╩ ╝╚╝ ╩ ╩ ╚═╝ ╩ ╩ ╩ ╩ ╩╚═ ╩
`
func main() {
flag.Parse()
if *confPath == "" || *endpoint == "" {
flag.Usage()
os.Exit(1)
}
fmt.Print(LINKSMART)
log.Printf("Starting Service Registrator")
// requiresAuth if authProvider is specified
var requiresAuth bool = (*authProvider != "")
service, err := LoadConfigFromFile(*confPath)
if err != nil {
log.Fatal("Unable to read service configuration from file: ", err)
}
if service.TTL == 0 {
log.Fatal("TTL must be larger than zero")
}
if service.ID == "" {
service.ID = uuid.NewV4().String()
log.Printf("ID not set, generated UUID: %s", service.ID)
} else {
log.Printf("Loaded service with ID: %s", service.ID)
}
var ticket *obtainer.Client
if requiresAuth {
// Setup ticket client
ticket, err = obtainer.NewClient(*authProvider, *authProviderURL, *authUser, *authPass, *serviceID)
if err != nil {
log.Fatal(err.Error())
}
}
// Launch the registration routine
_, _, err = client.RegisterServiceAndKeepalive(*endpoint, *service, ticket)
if err != nil {
log.Fatal(err.Error())
}
// Ctrl+C / Kill handling
handler := make(chan os.Signal, 1)
signal.Notify(handler, os.Interrupt, os.Kill)
<-handler
log.Println("Shutting down...")
}
// Loads service registration from a config file
func LoadConfigFromFile(confPath string) (*catalog.Service, error) {
f, err := ioutil.ReadFile(confPath)
if err != nil {
return nil, err
}
var service catalog.Service
err = json.Unmarshal(f, &service)
if err != nil {
return nil, fmt.Errorf("error parsing json: %s", err)
}
return &service, err
}