Skip to content

Commit 525d08d

Browse files
author
olevole
committed
playing/working on whitelist: wip
1 parent c713aaf commit 525d08d

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
src
33
./cbsd-mq-api
4+
cbsd-mq-api

main.go

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strconv"
1717
"strings"
1818
"sync"
19+
"io"
1920

2021
"github.com/gorilla/mux"
2122
"golang.org/x/crypto/ssh"
@@ -61,8 +62,48 @@ var (
6162
stopScript = flag.String("stop_script", "control-api", "CBSD target run script")
6263
serverUrl = flag.String("server_url", "http://127.0.0.1:65532", "Server URL for external requests")
6364
dbDir = flag.String("dbdir", "/var/db/cbsd-api", "db root dir")
65+
allowListFile = flag.String("allowlist", "/usr/local/etc/cbsd-mq-api.allow", "Path to PubKey whitelist")
6466
)
6567

68+
type AllowList struct {
69+
keyType string
70+
key string
71+
comment string
72+
cid string
73+
next *AllowList // link to the next records
74+
}
75+
76+
// linked struct
77+
type Feed struct {
78+
length int
79+
start *AllowList
80+
}
81+
82+
type MyFeeds struct {
83+
f *Feed
84+
}
85+
86+
func (f *Feed) Append(newAllow *AllowList) {
87+
if f.length == 0 {
88+
f.start = newAllow
89+
} else {
90+
currentPost := f.start
91+
for currentPost.next != nil {
92+
currentPost = currentPost.next
93+
}
94+
currentPost.next = newAllow
95+
}
96+
f.length++
97+
}
98+
99+
func newAllow(keyType string, key string, comment string) *AllowList {
100+
np := AllowList{keyType: keyType, key: key, comment: comment}
101+
// np.Response = ""
102+
// np.Time = 0
103+
return &np
104+
}
105+
106+
66107
// we need overwrite Content-Type here
67108
// https://stackoverflow.com/questions/59763852/can-you-return-json-in-golang-http-error
68109
func JSONError(w http.ResponseWriter, message string, code int) {
@@ -131,8 +172,68 @@ func main() {
131172
os.MkdirAll(*dbDir, 0770)
132173
}
133174

175+
176+
// WhiteList
177+
if !fileExists(*allowListFile) {
178+
fmt.Printf("no such allowList file, please check config/path: %s\n", allowListFile)
179+
os.Exit(1)
180+
}
181+
f := &Feed{}
182+
// var p *AllowList
183+
// loadconfig
184+
fd, err := os.Open(*allowListFile)
185+
if err != nil {
186+
panic(err)
187+
}
188+
defer fd.Close()
189+
190+
var keyType string
191+
var key string
192+
var comment string
193+
194+
195+
196+
for {
197+
_, err := fmt.Fscanf(fd,"%s %s %s",&keyType,&key,&comment)
198+
if err != nil {
199+
if err != io.EOF {
200+
//log.Fatal(err)
201+
break
202+
}
203+
}
204+
fmt.Printf("loaded: [%s %s %s]\n", keyType, key, comment)
205+
p := newAllow(keyType,key,comment)
206+
f.Append(p)
207+
}
208+
209+
fd.Close()
210+
211+
fmt.Printf("AllowList Length: %v\n", f.length)
212+
// currentAllow := f.start
213+
214+
var p *AllowList
215+
for i := 0; i < f.length; i++ {
216+
currentAllow := f.start
217+
p = currentAllow
218+
currentAllow = currentAllow.next
219+
ResultKeyType := (string(p.keyType))
220+
fmt.Println("ResultType: ", ResultKeyType)
221+
// if len(ResultAlias) < 1 {
222+
// ResultNameserver := (string(p.NameServer))
223+
// ResultNameserver = strings.Replace(ResultNameserver, ".", "_", -1)
224+
// ResultAlias = strings.Replace(ResultNameserver, ":", "_", -1)
225+
// }
226+
//
227+
// Result := fmt.Sprintf("check_dns_%s_%s: %d",ResultHost,ResultAlias,p.Time)
228+
// fmt.Println(Result)
229+
}
230+
231+
// setup: we need to pass Feed into handler function
232+
feeds := &MyFeeds{ f: f }
233+
134234
router := mux.NewRouter()
135-
router.HandleFunc("/api/v1/create/{InstanceId}", HandleClusterCreate).Methods("POST")
235+
// router.HandleFunc("/api/v1/create/{InstanceId}", HandleClusterCreate).Methods("POST")
236+
router.HandleFunc("/api/v1/create/{InstanceId}", feeds.HandleClusterCreate).Methods("POST")
136237
router.HandleFunc("/api/v1/status/{InstanceId}", HandleClusterStatus).Methods("GET")
137238
router.HandleFunc("/api/v1/start/{InstanceId}", HandleClusterStart).Methods("GET")
138239
router.HandleFunc("/api/v1/stop/{InstanceId}", HandleClusterStop).Methods("GET")
@@ -335,7 +436,10 @@ func getJname() string {
335436
return result
336437
}
337438

338-
func HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
439+
//func (feeds *MyFeeds) HandleClusterCluster(w http.ResponseWriter, r *http.Request) {
440+
//func HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
441+
func (feeds *MyFeeds) HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
442+
339443
var InstanceId string
340444
params := mux.Vars(r)
341445

@@ -415,6 +519,22 @@ func HandleClusterCreate(w http.ResponseWriter, r *http.Request) {
415519
//existance?
416520
// check for existance
417521
cid := md5.Sum(uid)
522+
523+
524+
//ALLOWED?
525+
var p *AllowList
526+
527+
currentAllow := feeds.f.start
528+
529+
for i := 0; i < feeds.f.length; i++ {
530+
p = currentAllow
531+
currentAllow = currentAllow.next
532+
ResultKeyType := (string(p.keyType))
533+
fmt.Println("ResultType: ", ResultKeyType)
534+
}
535+
536+
return
537+
418538
VmPathDir := fmt.Sprintf("%s/%x", *dbDir, cid)
419539

420540
if !fileExists(VmPathDir) {

0 commit comments

Comments
 (0)