-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth-utils.go
155 lines (135 loc) · 4.44 KB
/
auth-utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2015. Zuercher Hochschule fuer Angewandte Wissenschaften
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/*
* Author: Piyush Harsh,
* URL: piyush-harsh.info
*/
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/scalingdata/gcfg"
"github.com/gorilla/mux"
)
type Config struct {
Gatekeeper struct {
Port string
LogFile string
DbFile string
}
Tnova struct {
Defaultadmin string
Adminpassword string
}
}
type user_struct struct {
Username string `json:"username"`
Password string `json:"password"`
AdminFlag string `json:"isadmin"`
CapabilityList string `json:"accesslist"`
}
type dc_struct struct {
Dcname string `json:"dcname"`
AdminId string `json:"adminid"`
Password string `json:"password"`
ExtraInfo string `json:"extrainfo"`
}
type service_struct struct {
Shortname string `json:"shortname"`
Description string `json:"description"`
}
var (
Trace *log.Logger
Info *log.Logger
Warning *log.Logger
Error *log.Logger
MyFileTrace *log.Logger
MyFileInfo *log.Logger
MyFileWarning *log.Logger
MyFileError *log.Logger
staticMsgs [27]string
cfg Config
dbArg string
)
func main() {
err := gcfg.ReadFileInto(&cfg, "gatekeeper.cfg")
if err != nil {
log.Fatalf("Failed to parse gcfg data: %s", err)
os.Exit(1)
}
file, err := os.OpenFile(cfg.Gatekeeper.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open log file", "auth-utils.log", ":", err)
}
multi := io.MultiWriter(file, ioutil.Discard)
Initlogger(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, multi)
//logger has been initialized at this point
InitMsgs()
dbArg = "file:foo.db?cache=shared&mode=rwc"
dbArg = strings.Replace(dbArg, "foo.db", cfg.Gatekeeper.DbFile, 1)
dbCheck := CheckDB(dbArg)
if dbCheck {
MyFileInfo.Println("Table already exists in DB, nothing to do, proceeding normally.")
} else {
InitDB(dbArg)
}
r := mux.NewRouter().StrictSlash(false)
r.HandleFunc("/", HomeHandler)
users := r.Path("/admin/user/").Subrouter()
users.Methods("GET").HandlerFunc(UserListHandler)
users.Methods("POST").HandlerFunc(UserCreateHandler)
user := r.Path("/admin/user/{id}").Subrouter()
user.Methods("GET").HandlerFunc(UserDetailsHandler)
user.Methods("PUT").HandlerFunc(UserUpdateHandler)
user.Methods("DELETE").HandlerFunc(UserDeleteHandler)
password := r.Path("/password/{id}").Subrouter()
password.Methods("PUT").HandlerFunc(PasswordUpdateHandler)
auth := r.Path("/auth/{id}").Subrouter()
auth.Methods("GET").HandlerFunc(UserAuthHandler)
tokens := r.Path("/token/").Subrouter()
tokens.Methods("POST").HandlerFunc(TokenGenHandler)
tokenval := r.Path("/token/validate/{id}").Subrouter()
tokenval.Methods("GET").HandlerFunc(TokenValidateHandler)
services := r.Path("/admin/service/").Subrouter()
services.Methods("GET").HandlerFunc(ServiceListHandler)
services.Methods("POST").HandlerFunc(ServiceRegisterHandler)
dclist := r.Path("/admin/dc/").Subrouter()
dclist.Methods("GET").HandlerFunc(DcListHandler)
dclist.Methods("POST").HandlerFunc(DcCreateHandler)
dclist.Methods("DELETE").HandlerFunc(DcDeleteHandler)
dc := r.Path("/admin/dc/{id}").Subrouter()
dc.Methods("GET").HandlerFunc(DcDetailsHandler)
dc.Methods("PUT").HandlerFunc(DcUpdateHandler)
dc.Methods("DELETE").HandlerFunc(DcDeleteHandler)
portArg := ":port"
portArg = strings.Replace(portArg, "port", cfg.Gatekeeper.Port, 1)
MyFileInfo.Println("Starting server on", portArg)
http.ListenAndServe(portArg, r)
MyFileInfo.Println("Stopping server on", portArg)
}
func HomeHandler(out http.ResponseWriter, in *http.Request) {
out.Header().Set("Content-Type", "application/json")
out.WriteHeader(http.StatusOK) //200 status code
var jsonbody = staticMsgs[0]
fmt.Fprintln(out, jsonbody)
MyFileInfo.Println("Received request on URI:/ GET")
}