Skip to content

Commit bacb201

Browse files
authored
Updating webapp to latest scionlab dist (#147)
- new location of beacon logging - removed scion-apps args for local address - rework default args - updated service names, IA error check - loading sciond address from sd.toml - updating asTopo calls - updated parsing of paths - update app binary names, set sciond env - update health checks
1 parent 47ed345 commit bacb201

22 files changed

+526
-376
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/netsec-ethz/scion-apps
33
go 1.13
44

55
require (
6+
github.com/BurntSushi/toml v0.3.1
67
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
78
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
89
github.com/bclicn/color v0.0.0-20180711051946-108f2023dc84

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs
8282
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
8383
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
8484
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
85+
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
8586
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
8687
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
8788
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
@@ -172,11 +173,13 @@ github.com/onsi/gomega v1.8.1/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoT
172173
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
173174
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
174175
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
176+
github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible h1:MUIwjEiAMYk8zkXXUQeb5itrXF+HpS2pfxNsA2a7AiY=
175177
github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
176178
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
177179
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
178180
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
179181
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
182+
github.com/pkg/errors v0.8.2-0.20190227000051-27936f6d90f9 h1:PCj9X21C4pet4sEcElTfAi6LSl5ShkjE8doieLc+cbU=
180183
github.com/pkg/errors v0.8.2-0.20190227000051-27936f6d90f9/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
181184
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
182185
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

webapp/lib/bwcont.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,27 @@ func GetBwByTimeHandler(w http.ResponseWriter, r *http.Request, active bool) {
175175
}
176176
log.Debug("Requested data:", "bwTestResults", bwTestResults)
177177

178-
bwtestsJSON, err := json.Marshal(bwTestResults)
178+
bwtestsJSON, err := formatGraphResults(bwTestResults, active)
179179
if CheckError(err) {
180180
returnError(w, err)
181181
return
182182
}
183-
jsonBuf := []byte(`{ "graph": ` + string(bwtestsJSON))
183+
fmt.Fprint(w, bwtestsJSON)
184+
}
185+
186+
func formatGraphResults(v interface{}, active bool) (string, error) {
187+
testsJSON, err := json.Marshal(v)
188+
if CheckError(err) {
189+
return "", err
190+
}
191+
jsonBuf := []byte(`{ "graph": ` + string(testsJSON))
184192
json := []byte(`, "active": ` + strconv.FormatBool(active))
185193
jsonBuf = append(jsonBuf, json...)
186194
jsonBuf = append(jsonBuf, []byte(`}`)...)
187195

188-
// ensure % if any, is escaped correctly before writing to printf formatter
189-
fmt.Fprintf(w, strings.Replace(string(jsonBuf), "%", "%%", -1))
196+
// ensure % if any, is escaped correctly before writing to output
197+
jsonStr := strings.Replace(string(jsonBuf), "%", "%%", -1)
198+
return jsonStr, nil
190199
}
191200

192201
// WriteCmdCsv appends the cmd data (bwtest or echo) in csv-format to srcpath.

webapp/lib/config.go

Lines changed: 153 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package lib
1616

1717
import (
1818
"encoding/json"
19+
"flag"
1920
"fmt"
2021
"io/ioutil"
2122
"net"
@@ -25,37 +26,178 @@ import (
2526
"path/filepath"
2627
"regexp"
2728
"sort"
29+
"strconv"
2830
"strings"
2931

3032
log "github.com/inconshreveable/log15"
3133
. "github.com/netsec-ethz/scion-apps/webapp/util"
3234
)
3335

3436
// default params for localhost testing
35-
var cliPortDef = "30001"
36-
var serPortDefBwt = "30100"
37+
var listenAddrDef = "127.0.0.1"
38+
var listenPortDef = 8000
39+
var cliPortDef = 30001
40+
var serPortDef = 30100
3741
var serDefAddr = "127.0.0.2"
3842

3943
var cfgFileCliUser = "config/clients_user.json"
4044
var cfgFileSerUser = "config/servers_user.json"
4145
var cfgFileCliDef = "config/clients_default.json"
4246
var cfgFileSerDef = "config/servers_default.json"
4347

48+
// command argument constants
49+
var CMD_ADR = "a"
50+
var CMD_PRT = "p"
51+
var CMD_ART = "sabin"
52+
var CMD_WEB = "srvroot"
53+
var CMD_BRT = "r"
54+
var CMD_SCN = "sroot"
55+
var CMD_SCB = "sbin"
56+
var CMD_SCG = "sgen"
57+
var CMD_SCC = "sgenc"
58+
var CMD_SCL = "slogs"
59+
60+
// appsRoot is the root location of scionlab apps.
61+
var GOPATH = os.Getenv("GOPATH")
62+
63+
// scionRoot is the root location of the scion infrastructure.
64+
var DEF_SCIONDIR = path.Join(GOPATH, "src/github.com/scionproto/scion")
65+
4466
// UserSetting holds the serialized structure for persistent user settings
4567
type UserSetting struct {
46-
MyIA string `json:"myIa"`
68+
MyIA string `json:"myIa"`
69+
SDAddress string `json:"sdAddress"`
4770
}
4871

4972
type CmdOptions struct {
50-
StaticRoot, BrowseRoot, AppsRoot, ScionRoot, ScionBin, ScionGen, ScionGenCache, ScionLogs string
73+
Addr string
74+
Port int
75+
StaticRoot string
76+
BrowseRoot string
77+
AppsRoot string
78+
ScionRoot string
79+
ScionBin string
80+
ScionGen string
81+
ScionGenCache string
82+
ScionLogs string
83+
}
84+
85+
func (o *CmdOptions) AbsPathCmdOptions() {
86+
o.StaticRoot, _ = filepath.Abs(o.StaticRoot)
87+
o.BrowseRoot, _ = filepath.Abs(o.BrowseRoot)
88+
o.AppsRoot, _ = filepath.Abs(o.AppsRoot)
89+
o.ScionRoot, _ = filepath.Abs(o.ScionRoot)
90+
o.ScionBin, _ = filepath.Abs(o.ScionBin)
91+
o.ScionGen, _ = filepath.Abs(o.ScionGen)
92+
o.ScionGenCache, _ = filepath.Abs(o.ScionGenCache)
93+
o.ScionLogs, _ = filepath.Abs(o.ScionLogs)
94+
}
95+
96+
func isFlagUsed(name string) bool {
97+
found := false
98+
flag.Visit(func(f *flag.Flag) {
99+
if f.Name == name {
100+
found = true
101+
}
102+
})
103+
return found
104+
}
105+
106+
// defaultAppsRoot returns the directory containing the webapp executable as
107+
// the default base directory for the apps resources
108+
func defaultAppsRoot() string {
109+
exec, err := os.Executable()
110+
if err != nil {
111+
return ""
112+
}
113+
return path.Dir(exec)
114+
}
115+
116+
func defaultStaticRoot(appsRoot string) string {
117+
return path.Join(appsRoot, "../webapp/web")
118+
}
119+
120+
func defaultBrowseRoot(staticRoot string) string {
121+
return path.Join(staticRoot, "data")
122+
}
123+
124+
func defaultScionBin(scionRoot string) string {
125+
return path.Join(scionRoot, "bin")
126+
}
127+
128+
func defaultScionGen(scionRoot string) string {
129+
return path.Join(scionRoot, "gen")
130+
}
131+
132+
func defaultScionGenCache(scionRoot string) string {
133+
return path.Join(scionRoot, "gen-cache")
134+
}
135+
136+
func defaultScionLogs(scionRoot string) string {
137+
return path.Join(scionRoot, "logs")
138+
}
139+
140+
func ParseFlags() CmdOptions {
141+
addr := flag.String(CMD_ADR, listenAddrDef, "Address of server host.")
142+
port := flag.Int(CMD_PRT, listenPortDef, "Port of server host.")
143+
appsRoot := flag.String(CMD_ART, defaultAppsRoot(),
144+
"Path to execute the installed scionlab apps binaries")
145+
staticRoot := flag.String(CMD_WEB, defaultStaticRoot(*appsRoot),
146+
"Path to read/write web server files.")
147+
browseRoot := flag.String(CMD_BRT, defaultBrowseRoot(*staticRoot),
148+
"Root path to read/browse from, CAUTION: read-access granted from -a and -p.")
149+
scionRoot := flag.String(CMD_SCN, DEF_SCIONDIR,
150+
"Path to read SCION root directory of infrastructure")
151+
scionBin := flag.String(CMD_SCB, defaultScionBin(*scionRoot),
152+
"Path to execute SCION bin directory of infrastructure tools")
153+
scionGen := flag.String(CMD_SCG, defaultScionGen(*scionRoot),
154+
"Path to read SCION gen directory of infrastructure config")
155+
scionGenCache := flag.String(CMD_SCC, defaultScionGenCache(*scionRoot),
156+
"Path to read SCION gen-cache directory of infrastructure run-time config")
157+
scionLogs := flag.String(CMD_SCL, defaultScionLogs(*scionRoot),
158+
"Path to read SCION logs directory of infrastructure logging")
159+
flag.Parse()
160+
// recompute root args to use the proper relative defaults if undefined
161+
if !isFlagUsed(CMD_WEB) {
162+
*staticRoot = defaultStaticRoot(*appsRoot)
163+
}
164+
if !isFlagUsed(CMD_BRT) {
165+
*browseRoot = defaultBrowseRoot(*staticRoot)
166+
}
167+
168+
if isFlagUsed(CMD_SCN) {
169+
if !isFlagUsed(CMD_SCB) {
170+
*scionBin = defaultScionBin(*scionRoot)
171+
}
172+
if !isFlagUsed(CMD_SCG) {
173+
*scionGen = defaultScionGen(*scionRoot)
174+
}
175+
if !isFlagUsed(CMD_SCC) {
176+
*scionGenCache = defaultScionGenCache(*scionRoot)
177+
}
178+
if !isFlagUsed(CMD_SCL) {
179+
*scionLogs = defaultScionLogs(*scionRoot)
180+
}
181+
}
182+
options := CmdOptions{*addr, *port, *staticRoot, *browseRoot, *appsRoot,
183+
*scionRoot, *scionBin, *scionGen, *scionGenCache, *scionLogs}
184+
options.AbsPathCmdOptions()
185+
return options
51186
}
52187

53188
// WriteUserSetting writes the settings to disk.
54-
func WriteUserSetting(options *CmdOptions, settings UserSetting) {
189+
func WriteUserSetting(options *CmdOptions, settings *UserSetting) {
55190
cliUserFp := path.Join(options.StaticRoot, cfgFileCliUser)
191+
192+
// writing myIA means we have to retrieve sciond's tcp address too
193+
// since sciond's address may be autognerated
194+
config, err := LoadSciondConfig(options, settings.MyIA)
195+
CheckError(err)
196+
settings.SDAddress = config.SD.Address
56197
settingsJSON, _ := json.Marshal(settings)
57198

58-
err := ioutil.WriteFile(cliUserFp, settingsJSON, 0644)
199+
log.Info("Updating...", "UserSetting", string(settingsJSON))
200+
err = ioutil.WriteFile(cliUserFp, settingsJSON, 0644)
59201
CheckError(err)
60202
}
61203

@@ -153,7 +295,8 @@ func GenServerNodeDefaults(options *CmdOptions, localIAs []string) {
153295
// use all localhost endpoints as possible servers for bwtester as least
154296
ia := strings.Replace(localIAs[i], "_", ":", -1)
155297
json := []byte(`{"name":"lo ` + ia + `","isdas":"` + ia +
156-
`", "addr":"` + serDefAddr + `","port":` + serPortDefBwt + `}`)
298+
`", "addr":"` + serDefAddr + `","port":` + strconv.Itoa(serPortDef) +
299+
`}`)
157300
jsonBuf = append(jsonBuf, json...)
158301
if i < (len(localIAs) - 1) {
159302
jsonBuf = append(jsonBuf, []byte(`,`)...)
@@ -168,7 +311,6 @@ func GenServerNodeDefaults(options *CmdOptions, localIAs []string) {
168311
// SCION addresses as json
169312
func GenClientNodeDefaults(options *CmdOptions, cisdas string) {
170313
cliFp := path.Join(options.StaticRoot, cfgFileCliDef)
171-
cport := cliPortDef
172314

173315
// find interface addresses
174316
jsonBuf := []byte(`{ "all": [ `)
@@ -192,7 +334,8 @@ func GenClientNodeDefaults(options *CmdOptions, cisdas string) {
192334
cname := i.Name
193335
caddr := ipnet.IP.String()
194336
jsonInterface := []byte(`{"name":"` + cname + `", "isdas":"` +
195-
cisdas + `", "addr":"` + caddr + `","port":` + cport + `}`)
337+
cisdas + `", "addr":"` + caddr + `","port":` +
338+
strconv.Itoa(cliPortDef) + `}`)
196339
jsonBuf = append(jsonBuf, jsonInterface...)
197340
idx++
198341
}
@@ -223,5 +366,5 @@ func GetNodesHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions
223366
}
224367
raw, err := ioutil.ReadFile(fp)
225368
CheckError(err)
226-
fmt.Fprintf(w, string(raw))
369+
fmt.Fprint(w, string(raw))
227370
}

webapp/lib/echocont.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package lib
1616

1717
import (
18-
"encoding/json"
1918
"fmt"
2019
"net/http"
2120
"regexp"
@@ -133,16 +132,10 @@ func GetEchoByTimeHandler(w http.ResponseWriter, r *http.Request, active bool) {
133132
}
134133
log.Debug("Requested data:", "echoResults", echoResults)
135134

136-
echoJSON, err := json.Marshal(echoResults)
135+
echoJSON, err := formatGraphResults(echoResults, active)
137136
if CheckError(err) {
138137
returnError(w, err)
139138
return
140139
}
141-
jsonBuf := []byte(`{ "graph": ` + string(echoJSON))
142-
json := []byte(`, "active": ` + strconv.FormatBool(active))
143-
jsonBuf = append(jsonBuf, json...)
144-
jsonBuf = append(jsonBuf, []byte(`}`)...)
145-
146-
// ensure % if any, is escaped correctly before writing to printf formatter
147-
fmt.Fprintf(w, strings.Replace(string(jsonBuf), "%", "%%", -1))
140+
fmt.Fprint(w, echoJSON)
148141
}

webapp/lib/health.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ type ResHealthCheck struct {
5959

6060
// HealthCheckHandler handles calling the default health-check scripts and
6161
// returning the json-formatted results of each script.
62-
func HealthCheckHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions, ia string) {
62+
func HealthCheckHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions, settings UserSetting) {
6363
hcResFp := path.Join(options.StaticRoot, resFileHealthCheck)
6464
// read specified tests from json definition
6565
fp := path.Join(options.StaticRoot, defFileHealthCheck)
6666
raw, err := ioutil.ReadFile(fp)
6767
if CheckError(err) {
68-
fmt.Fprintf(w, `{ "err": "`+err.Error()+`" }`)
68+
fmt.Fprint(w, `{ "err": "`+err.Error()+`" }`)
6969
return
7070
}
7171
log.Debug("HealthCheckHandler", "resFileHealthCheck", string(raw))
@@ -81,7 +81,7 @@ func HealthCheckHandler(w http.ResponseWriter, r *http.Request, options *CmdOpti
8181
var tests DefTests
8282
err = json.Unmarshal([]byte(raw), &tests)
8383
if CheckError(err) {
84-
fmt.Fprintf(w, `{ "err": "`+err.Error()+`" }`)
84+
fmt.Fprint(w, `{ "err": "`+err.Error()+`" }`)
8585
return
8686
}
8787

@@ -101,7 +101,8 @@ func HealthCheckHandler(w http.ResponseWriter, r *http.Request, options *CmdOpti
101101
pass := true
102102
log.Info(test.Script + ": " + test.Desc)
103103
// execute script
104-
cmd := exec.Command("bash", test.Script, ia)
104+
cmd := exec.Command("bash", test.Script,
105+
settings.MyIA, settings.SDAddress)
105106
cmd.Dir = filepath.Dir(fp)
106107
cmd.Env = append(os.Environ(), envvars...)
107108
var stdout, stderr bytes.Buffer
@@ -160,6 +161,6 @@ func HealthCheckHandler(w http.ResponseWriter, r *http.Request, options *CmdOpti
160161
CheckError(err)
161162
}
162163

163-
// ensure all escaped correctly before writing to printf formatter
164-
fmt.Fprintf(w, string(jsonRes))
164+
// ensure all escaped correctly before writing to output
165+
fmt.Fprint(w, string(jsonRes))
165166
}

webapp/lib/image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func FindImageInfoHandler(w http.ResponseWriter, r *http.Request, options *CmdOp
8181
return
8282
}
8383
fileText := imgFilename
84-
fmt.Fprintf(w, fileText)
84+
fmt.Fprint(w, fileText)
8585
}
8686

8787
// FindImageHandler locating most recent image formatting it for graphic display in response.
88-
func FindImageHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions, browserAddr string, port int) {
88+
func FindImageHandler(w http.ResponseWriter, r *http.Request, options *CmdOptions) {
8989
filesDir := path.Join(options.StaticRoot, "data/images")
9090
imgFilename, _ := findNewestFileExt(filesDir, regexImageFiles)
9191
if imgFilename == "" {
@@ -113,6 +113,6 @@ func FindImageHandler(w http.ResponseWriter, r *http.Request, options *CmdOption
113113
panic("Unhandled image type!")
114114
}
115115
CheckError(err)
116-
url := fmt.Sprintf("http://%s:%d/%s/%s", browserAddr, port, "data/images", imgFilename)
116+
url := fmt.Sprintf("http://%s:%d/%s/%s", options.Addr, options.Port, "data/images", imgFilename)
117117
writeJpegTemplate(w, &rawImage, url)
118118
}

0 commit comments

Comments
 (0)