-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wesuuu
committed
Nov 12, 2021
1 parent
c9b5e62
commit 7fa04fa
Showing
8 changed files
with
244 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func ApiKeyAuthMiddleware(next http.Handler) http.Handler { | ||
apiKey := os.Getenv("API_KEY") | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Do stuff here | ||
token := r.Header.Get("Authorization") | ||
if token != apiKey { | ||
http.Error(w, "Invalid API Key", http.StatusForbidden) | ||
} else { | ||
// Call the next handler, which can be another middleware in the chain, or the final handler. | ||
next.ServeHTTP(w, r) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/ghodss/yaml" | ||
) | ||
|
||
const configFile = "/workspaces/deployment-controller/deployment-controller.yaml" | ||
|
||
type KeyValue struct { | ||
Key string `yaml:"Key"` | ||
Value string `yaml:"Value"` | ||
} | ||
|
||
// cookie data from config file | ||
type Cookie struct { | ||
Expiration string `yaml:"Expiration"` | ||
CanaryPercent float32 `yaml:"CanaryPercent"` | ||
IfSuccessful KeyValue `yaml:"IfSuccessful"` | ||
IfFail KeyValue `yaml:"IfFail"` | ||
} | ||
|
||
type View struct { | ||
ShowSuccess bool `yaml:"ShowSuccess"` | ||
ShowFail bool `yaml:"ShowFail"` | ||
} | ||
|
||
type Logging struct { | ||
Disable bool `yaml:"Disable"` | ||
} | ||
|
||
type App struct { | ||
Name string `yaml:"Name"` | ||
Disable bool `yaml:"Disable"` | ||
CookieInfo Cookie `yaml:"CookieInfo"` | ||
View View `yaml:"View"` | ||
Logging Logging `yaml:"Logging"` | ||
} | ||
|
||
type Config struct { | ||
Apps []App `yaml:"Apps"` | ||
Port int `yaml:"Port"` | ||
} | ||
|
||
func ReadConfig() (Config, error) { | ||
// set path, use /workspaces/.. if unspecified | ||
configPath := os.Getenv("APP_CONFIG_PATH") | ||
if configPath == "" { | ||
configPath = "/workspaces/deployment-controller/deployment-controller.yaml" | ||
} | ||
config := Config{} | ||
configYaml, err := ioutil.ReadFile(configPath) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
err = yaml.Unmarshal(configYaml, &config) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
func UpdateConfig(app App) error { | ||
config, err := ReadConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appFound := false | ||
for i, configapp := range config.Apps { | ||
if app.Name == configapp.Name { | ||
configapp = app | ||
config.Apps[i] = configapp | ||
appFound = true | ||
} | ||
} | ||
|
||
if !appFound { | ||
return errors.New("could not find app") | ||
} | ||
|
||
data, err := yaml.Marshal(config) | ||
if err != nil { | ||
return err | ||
} | ||
err = ioutil.WriteFile(configFile, data, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
apps: | ||
- appName: jupyterhub | ||
disable: false | ||
cookieInfo: | ||
expiration: 48h | ||
canaryPercent: .90 | ||
ifSuccessful: # less than percent | ||
key: a | ||
value: a | ||
ifFail: | ||
key: b | ||
value: b | ||
view: # related to showing the UI elements to users (in this case, the stationary banner on datahub) | ||
showSuccess: true | ||
showFail: true | ||
logging: | ||
disable: false | ||
port: 8080 | ||
Apps: | ||
- CookieInfo: | ||
CanaryPercent: 0.9 | ||
Expiration: 48h | ||
IfFail: | ||
Key: b | ||
Value: b | ||
IfSuccessful: | ||
Key: a | ||
Value: a | ||
Disable: false | ||
Logging: | ||
Disable: false | ||
Name: jupyterhub | ||
View: | ||
ShowFail: true | ||
ShowSuccess: true | ||
Port: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
curl -XPUT -H "Authorization: test" -d '{"CookieInfo":{"CanaryPercent":0.9,"Expiration":"48h","IfFail":{"Key":"b","Value":"b"},"IfSuccessful":{"Key":"a","Value":"a"}},"Disable":false,"Logging":{"Disable":false},"Name":"jupyterhub","View":{"ShowFail":false,"ShowSuccess":true}}' http://localhost:8080/admin/jupyterhub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.