Skip to content

Commit f6f55fb

Browse files
committed
overhaul, many changes
1 parent 2fd8eb4 commit f6f55fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1406
-23610
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ Web page using Gorilla web toolkit and MongoDB Driver.
1010

1111
# Deploy
1212

13+
## Environment Variables
14+
15+
Example of the environment variables needed to get started:
16+
17+
```conf
18+
MONGOURL = "mongodb://localhost:27017"
19+
MONGO_DB_NAME = "test"
20+
MONGOSTORE_SESSION_TTL = "1200"
21+
MONGOSTORE_HTTPS_ONLY = "false"
22+
GORILLA_SESSION_AUTH_KEY = "SuperSecret32ByteKey"
23+
GORILLA_SESSION_ENC_KEY = "SuperSecret16ByteKey"
24+
LDAP_SERVER = "LDAPSSL"
25+
LDAP_PORT = "636"
26+
LDAP_BIND_DN = "SuperSecretBindUsername"
27+
LDAP_BIND_PASS = "SuperSecretBindPassword"
28+
LDAP_USER_BASE_DN = "OU=Users,DC=go-stuff,DC=ca"
29+
LDAP_USER_SEARCH_ATTR = "CN"
30+
LDAP_GROUP_BASE_DN = "OU=Groups,DC=go-stuff,DC=ca"
31+
LDAP_GROUP_OBJECT_CLASS = "group"
32+
LDAP_GROUP_SEARCH_ATTR = "member"
33+
LDAP_GROUP_SEARCH_FULL = "true"
34+
ADMIN_AD_GROUP = "ADAdminGroup"
35+
```
36+
1337
## Kubernetes
1438

1539
To deploy in Kubernetes run the following in the root dir:

controllers/auditHandler.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
"log"
6+
"net/http"
7+
8+
"time"
9+
10+
"github.com/go-stuff/grpc/api"
11+
)
12+
13+
func auditList100Handler(w http.ResponseWriter, r *http.Request) {
14+
// get audit
15+
session, err := store.Get(r, "session")
16+
if err != nil {
17+
log.Printf("ERROR > controllers/auditHandler.go > auditList100Handler() > store.Get(): %s\n", err.Error())
18+
http.Error(w, err.Error(), http.StatusInternalServerError)
19+
return
20+
}
21+
22+
// display audit
23+
log.Printf("INFO > controllers/auditHandler.go > auditList100Handler() > audit: %v %v\n", session.Values["_id"], session.Values["username"])
24+
25+
// call api to get a slice of sessions
26+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
27+
defer cancel()
28+
29+
auditSvc := api.NewAuditServiceClient(apiClient)
30+
31+
auditReq := new(api.AuditList100Req)
32+
auditRes, err := auditSvc.List100(ctx, auditReq)
33+
if err != nil {
34+
log.Printf("ERROR > controllers/auditHandler.go > auditList100Handler() > auditSvc.List(): %s\n", err.Error())
35+
http.Error(w, err.Error(), http.StatusInternalServerError)
36+
return
37+
}
38+
39+
render(w, r, "auditList.html",
40+
struct {
41+
Audit []*api.Audit
42+
}{
43+
Audit: auditRes.Audits,
44+
},
45+
)
46+
}

controllers/controllers.go

Lines changed: 83 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/go-stuff/mongostore"
1515
"github.com/golang/protobuf/ptypes/timestamp"
1616
"github.com/gorilla/mux"
17-
"github.com/gorilla/sessions"
1817
"go.mongodb.org/mongo-driver/mongo"
1918
"google.golang.org/grpc"
2019

@@ -46,7 +45,17 @@ func Init(mongoclient *mongo.Client, mongostore *mongostore.MongoStore, apiclien
4645

4746
router = initRouter()
4847

49-
CompileRoutes()
48+
// seed roles
49+
err = roleSeed()
50+
if err != nil {
51+
log.Fatal(err)
52+
}
53+
54+
// seed routes
55+
err = routeSeed()
56+
if err != nil {
57+
log.Fatal(err)
58+
}
5059

5160
return router
5261
}
@@ -57,8 +66,14 @@ func initTemplates() error {
5766
// initialize the content files templates map
5867
templates = make(map[string]*template.Template)
5968

69+
// build templates with auth and content
70+
err := initTemplatesWithAuthAndContent()
71+
if err != nil {
72+
return err
73+
}
74+
6075
// build templates with content
61-
err := initTemplatesWithContent()
76+
err = initTemplatesWithContent()
6277
if err != nil {
6378
return err
6479
}
@@ -69,16 +84,35 @@ func initTemplates() error {
6984
return err
7085
}
7186

72-
// // build noauth template
73-
// err = initTemplateNoAuth()
74-
// if err != nil {
75-
// return err
76-
// }
87+
return nil
88+
}
89+
90+
func initTemplatesWithAuthAndContent() error {
91+
log.Println("INFO > controllers/controllers.go > initTemplatesWithAuthAndContent()")
92+
93+
layout = template.New("mainAuthContent.html")
94+
95+
layout.Funcs(timestampFM())
96+
layout.Funcs(permissionFM(nil))
97+
98+
// check the validity of login.html by parsing
99+
_, err := layout.ParseFiles(
100+
"./templates/layout/mainAuthContent.html",
101+
"./templates/layout/head.html",
102+
"./templates/layout/header.html",
103+
"./templates/layout/footer.html",
104+
"./templates/layout/script.html",
105+
)
106+
if err != nil {
107+
return err
108+
}
109+
110+
// recurse content files templates and build separate templates for each of them
111+
filepath.Walk("./templates/mainAuthContent", walkTemplatesPath)
77112

78113
return nil
79114
}
80115

81-
// <html> head, header, content, footer </html
82116
func initTemplatesWithContent() error {
83117
log.Println("INFO > controllers/controllers.go > initTemplatesWithContent()")
84118

@@ -88,21 +122,24 @@ func initTemplatesWithContent() error {
88122
layout.Funcs(permissionFM(nil))
89123

90124
// check the validity of login.html by parsing
91-
layout.ParseFiles(
125+
_, err := layout.ParseFiles(
92126
"./templates/layout/mainContent.html",
93127
"./templates/layout/head.html",
94128
"./templates/layout/header.html",
95-
"./templates/layout/bypass.html",
129+
"./templates/layout/logout.html",
96130
"./templates/layout/footer.html",
131+
"./templates/layout/script.html",
97132
)
133+
if err != nil {
134+
return err
135+
}
98136

99137
// recurse content files templates and build separate templates for each of them
100138
filepath.Walk("./templates/mainContent", walkTemplatesPath)
101139

102140
return nil
103141
}
104142

105-
// <html> head, header, menu, content, footer </html
106143
func initTemplatesWithNavAndContent() error {
107144
log.Println("INFO > controllers/controllers.go > initTemplatesWithNavAndContent()")
108145
//var err error
@@ -113,53 +150,25 @@ func initTemplatesWithNavAndContent() error {
113150
layout.Funcs(permissionFM(nil))
114151

115152
// check the validity of the files that make up layout.html by parsing
116-
layout.ParseFiles(
153+
_, err := layout.ParseFiles(
117154
"./templates/layout/mainNavContent.html",
118155
"./templates/layout/head.html",
119156
"./templates/layout/header.html",
120-
"./templates/layout/bypass.html",
121-
"./templates/layout/logout.html",
122157
"./templates/layout/nav.html",
158+
"./templates/layout/logout.html",
123159
"./templates/layout/footer.html",
160+
"./templates/layout/script.html",
124161
)
125-
// if err != nil {
126-
// return err
127-
// }
162+
if err != nil {
163+
return err
164+
}
128165

129166
// recurse content files templates and build separate templates for each of them
130167
filepath.Walk("./templates/mainMenuContent", walkTemplatesPath)
131168

132169
return nil
133170
}
134171

135-
// // <html> head, header, menu, content, footer </html
136-
// func initTemplateNoAuth() error {
137-
// log.Println("INFO > controllers/controllers.go > initTemplateNoAuth()")
138-
// //var err error
139-
140-
// layout = template.New("mainContent.html")
141-
142-
// layout.Funcs(timestampFM())
143-
// layout.Funcs(permissionFM(nil))
144-
145-
// // check the validity of the files that make up layout.html by parsing
146-
// layout.ParseFiles(
147-
// "./templates/layout/mainContent.html",
148-
// "./templates/layout/head.html",
149-
// "./templates/layout/header.html",
150-
// "./templates/layout/bypass.html",
151-
// "./templates/layout/footer.html",
152-
// )
153-
// // if err != nil {
154-
// // return err
155-
// // }
156-
157-
// // recurse content files templates and build separate templates for each of them
158-
// filepath.Walk("./templates/mainNoAuth", walkTemplatesPath)
159-
160-
// return nil
161-
// }
162-
163172
// recurse a directory and build templates
164173
func walkTemplatesPath(path string, fileInfo os.FileInfo, err error) error {
165174

@@ -203,16 +212,6 @@ func walkTemplatesPath(path string, fileInfo os.FileInfo, err error) error {
203212
func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{}) {
204213
log.Printf("INFO > controllers/controllers.go > render(): %s", tmpl)
205214

206-
// var tpl bytes.Buffer
207-
// e := templates[tmpl].Execute(&tpl, data)
208-
// if e != nil {
209-
// log.Println(tmpl)
210-
211-
// }
212-
// log.Println(e)
213-
// log.Printf("\ntmpl: %v\n", templates[tmpl])
214-
// log.Printf("\nbytes: %v\n", tpl.String())
215-
216215
// Set the content type.
217216
w.Header().Set("Content-Type", "text/html")
218217

@@ -223,7 +222,6 @@ func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{
223222
err := templates[tmpl].Execute(w, data)
224223
if err != nil {
225224
log.Printf("ERROR > controllers.go > render(): %v", err)
226-
//fmt.Println(err)
227225
}
228226
}
229227

@@ -233,27 +231,30 @@ func initRouter() *mux.Router {
233231
router := mux.NewRouter()
234232

235233
// System Routes
236-
router.HandleFunc("/session/list", sessionListHandler).Methods("GET")
234+
router.HandleFunc("/audit/list100", auditList100Handler).Methods("GET")
235+
236+
router.HandleFunc("/login", loginHandler).Methods("GET", "POST")
237+
router.HandleFunc("/logout", loginHandler).Methods("GET")
238+
239+
router.HandleFunc("/noauth", noauthHandler).Methods("GET")
237240

238241
router.HandleFunc("/role/list", roleListHandler).Methods("GET")
239242
router.HandleFunc("/role/create", roleCreateHandler).Methods("GET", "POST")
240243
router.HandleFunc("/role/read/{id}", roleReadHandler).Methods("GET")
241244
router.HandleFunc("/role/update/{id}", roleUpdateHandler).Methods("GET", "POST")
242-
router.HandleFunc("/role/delete/{id}", roleDeleteHandler).Methods("GET")
245+
router.HandleFunc("/role/delete/{id}", roleDeleteHandler).Methods("POST")
243246

244247
router.HandleFunc("/route/list", routeListHandler).Methods("GET", "POST")
245248

249+
router.HandleFunc("/session/list", sessionListHandler).Methods("GET")
250+
246251
router.HandleFunc("/user/list", userListHandler).Methods("GET")
252+
router.HandleFunc("/user/read/{id}", userReadHandler).Methods("GET")
247253
router.HandleFunc("/user/update/{id}", userUpdateHandler).Methods("GET", "POST")
248254
router.HandleFunc("/user/delete/{id}", userDeleteHandler).Methods("GET")
249255

250-
router.HandleFunc("/login", loginHandler).Methods("GET", "POST")
251-
router.HandleFunc("/logout", loginHandler).Methods("GET")
252-
253-
router.HandleFunc("/noauth", noauthHandler).Methods("GET")
254-
255256
// App Routes
256-
router.HandleFunc("/", rootHandler).Methods("GET", "POST")
257+
router.HandleFunc("/", homeHandler).Methods("GET", "POST")
257258
router.HandleFunc("/home", homeHandler).Methods("GET")
258259

259260
router.HandleFunc("/server/list", serverListHandler).Methods("GET")
@@ -265,27 +266,6 @@ func initRouter() *mux.Router {
265266
return router
266267
}
267268

268-
// func initPermissions() error {
269-
// // call api to get a slice of permissions
270-
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
271-
// defer cancel()
272-
// svc := api.NewPermissionServiceClient(apiClient)
273-
// req := new(api.PermissionSliceReq)
274-
// slice, err := svc.Slice(ctx, req)
275-
// if err != nil {
276-
// log.Printf("controllers/controllers.go > ERROR > svc.Slice(): %s\n", err.Error())
277-
// return err
278-
// }
279-
280-
// permissions = make(map[string]string)
281-
282-
// for _, permission := range slice.Permissions {
283-
// permissions[permission.RoleID] = permission.Route
284-
// }
285-
286-
// return nil
287-
// }
288-
289269
// format timestamps
290270
func timestampFM() template.FuncMap {
291271
return template.FuncMap{
@@ -320,9 +300,6 @@ func permissionFM(r *http.Request) template.FuncMap {
320300
return false
321301
}
322302

323-
//currentRoute := mux.CurrentRoute(r)
324-
//pathTemplate, _ := currentRoute.GetPathTemplate()
325-
326303
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
327304
defer cancel()
328305

@@ -350,8 +327,24 @@ func permissionFM(r *http.Request) template.FuncMap {
350327
}
351328

352329
// addNotification adds a notification message to session.Values
353-
func addNotification(session *sessions.Session, notification string) {
330+
func addNotification(w http.ResponseWriter, r *http.Request, notification string) {
331+
// get session
332+
session, err := store.Get(r, "session")
333+
if err != nil {
334+
log.Printf("ERROR > controllers/controllers.go > addNotification() > store.Get(): %s\n", err.Error())
335+
http.Error(w, err.Error(), http.StatusInternalServerError)
336+
return
337+
}
338+
354339
session.Values["notification"] = notification
340+
341+
// save session
342+
err = session.Save(r, w)
343+
if err != nil {
344+
log.Printf("ERROR > controllers/controllers.go > addNotification() > session.Save(): %s\n", err.Error())
345+
http.Error(w, err.Error(), http.StatusInternalServerError)
346+
return
347+
}
355348
}
356349

357350
// getNotification returns a notification from session.Values if

0 commit comments

Comments
 (0)