-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (158 loc) · 4.44 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"ttyauthz/authorization"
)
const (
pluginName = "ttyauthz"
pluginFolder = "/home/danleyb2/Desktop/tmp" // "/run/docker/plugins"
)
// Manifest lists what a plugin implements.
type Manifest struct {
// List of subsystem the plugin implements.
Implements []string
}
const (
host = "localhost"
port = 5432
user = "postgres"
password = "123456"
dbname = "devops_tmp"
)
type User struct {
ID int
Email string
Password string
}
func main() {
fmt.Println("server starting..")
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, dbErr := sql.Open("postgres", psqlInfo)
if dbErr != nil {
panic(dbErr)
}
defer db.Close()
dbErr = db.Ping()
if dbErr != nil {
panic(dbErr)
}
fmt.Print("Database Connection initiated")
// Query
// Create an empty user and make the sql query (using $1 for the parameter)
var myUser User
userSql := "SELECT id, email, password FROM authentication_user WHERE id = $1"
dbErr = db.QueryRow(userSql, 1).Scan(&myUser.ID, &myUser.Email, &myUser.Password)
if dbErr != nil {
log.Fatal("Failed to execute query: ", dbErr)
//panic(dbErr)
}
fmt.Printf("\n User: %s, %s \n", myUser.Email, myUser.Password)
//
router := mux.NewRouter()
router.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
b, err := json.Marshal(Manifest{Implements: []string{"authz"}})
if err != nil {
writeErr(w, err)
return
}
w.Write(b)
})
router.HandleFunc(fmt.Sprintf("/%s", authorization.AuthZApiRequest), func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeErr(w, err)
return
}
var authReq authorization.Request
err = json.Unmarshal(body, &authReq)
if err != nil {
writeErr(w, err)
return
}
// authZRes := a.authorizer.AuthZReq(&authReq)
var authZRes authorization.Response
log.Fatal("Received AuthZ request, method: '%s', url: '%s'", authReq.RequestMethod, authReq.RequestURI)
url2, err := url.Parse(authReq.RequestURI)
if err != nil {
authZRes = authorization.Response{
Allow: false,
Msg: fmt.Sprintf("invalid request URI: %s", err.Error()),
}
} else {
// action := core.ParseRoute(authReq.RequestMethod, url.Path)
// https://github.com/twistlock/authz/blob/master/core/route_parser.go
fmt.Println(fmt.Sprintf("Request URI: %s", url2.Path))
action := "ActionX"
authZRes = authorization.Response{
Allow: false,
Msg: fmt.Sprintf("no policy applied (user: '%s' action: '%s')", authReq.User, action),
}
}
writeResponse(w, &authZRes)
})
router.HandleFunc(fmt.Sprintf("/%s", authorization.AuthZApiResponse), func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
writeErr(w, err)
return
}
var authReq authorization.Request
err = json.Unmarshal(body, &authReq)
if err != nil {
writeErr(w, err)
return
}
// authZRes := a.authorizer.AuthZRes(&authReq)
var authZRes authorization.Response = authorization.Response{Allow: true}
writeResponse(w, &authZRes)
})
// if _, err := os.Stat(pluginFolder); os.IsNotExist(err) {
// fmt.Println("Creating plugins folder %q", pluginName)
// err = os.MkdirAll("/run/docker/plugins/", 0750)
// if err != nil {
// log.Fatal( err)
// }
// }
pluginPath := fmt.Sprintf("%s/%s.sock", pluginFolder, pluginName)
os.Remove(pluginPath)
var listener net.Listener
var err error
listener, err = net.ListenUnix("unix", &net.UnixAddr{Name: pluginPath, Net: "unix"})
if err != nil {
// return err
log.Fatal(err)
} else {
defer listener.Close()
http.Serve(listener, router)
}
}
// writeResponse writes the authZPlugin response to response writer
func writeResponse(w http.ResponseWriter, authZRes *authorization.Response) {
data, err := json.Marshal(authZRes)
if err != nil {
log.Fatal("Failed to marshel authz response %q", err.Error())
} else {
w.Write(data)
}
if authZRes == nil || authZRes.Err != "" {
w.WriteHeader(http.StatusInternalServerError)
}
}
// writeErr writes the authZPlugin error response to response writer
func writeErr(w http.ResponseWriter, err error) {
writeResponse(w, &authorization.Response{Err: err.Error()})
}