From be30d4e8ce769e75b898e9417e15a01cb3004c3e Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Wed, 1 Jan 2014 22:23:45 -0500 Subject: [PATCH] Fix some failed kensa tests --- addon-manifest.json | 6 +++--- app.yaml | 2 +- herokusql.go | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/addon-manifest.json b/addon-manifest.json index 491cd17..fbd5c84 100644 --- a/addon-manifest.json +++ b/addon-manifest.json @@ -1,7 +1,7 @@ { "id": "googlecloudsql", "api": { - "config_vars": [ "MYSQL_URL" ], + "config_vars": [ "GOOGLECLOUDSQL_URL" ], "regions": [ "us" ], "password": "f1937226ef79503baddc427190207e5d", "sso_salt": "4f8954d728b09b5d9e7f5ec327c539ed", @@ -10,8 +10,8 @@ "sso_url": "https://herokusql.appspot.com/sso/login" }, "test": { - "base_url": "http://localhost:8080/heroku/resources", - "sso_url": "http://localhost:8080/sso/login" + "base_url": "https://test.herokusql.appspot.com/heroku/resources", + "sso_url": "https://test.herokusql.appspot.com/sso/login" } } } diff --git a/app.yaml b/app.yaml index 72e048a..1838b2b 100644 --- a/app.yaml +++ b/app.yaml @@ -1,5 +1,5 @@ application: herokusql -version: 1 +version: test runtime: go api_version: go1 diff --git a/herokusql.go b/herokusql.go index e06e2cc..974a5a9 100644 --- a/herokusql.go +++ b/herokusql.go @@ -8,6 +8,7 @@ import ( "appengine/urlfetch" "bytes" + "encoding/base64" "encoding/json" "errors" "io/ioutil" @@ -21,7 +22,7 @@ const ( authScope = "https://www.googleapis.com/auth/sqlservice.admin" insertURL = "https://www.googleapis.com/sql/v1beta3/projects/" + projectName + "/instances" bufSize = 1 << 13 // 8KB - basePath = "/heroku/resource" + basePath = "/heroku/resources" ) var instanceExists = errors.New("App already provisioned") @@ -31,6 +32,9 @@ var tierMap = map[string]string{ "river": "D4", "deluge": "D16", "torrent": "D32", + + // For testing + "test": "D0", } func init() { @@ -38,19 +42,36 @@ func init() { } func handler(w http.ResponseWriter, r *http.Request) { - if p, set := r.URL.User.Password(); !set { - http.Error(w, "Unauthenticated", http.StatusUnauthorized) - return - } else if p != password { - http.Error(w, "Forbidden", http.StatusForbidden) + if sc := checkAuth(r); sc != http.StatusOK { + http.Error(w, "", sc) return } - if r.URL.Path == basePath { + if r.URL.Path == basePath && r.Method == "POST" { provision(w, r) } } +func checkAuth(r *http.Request) int { + s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) + if len(s) != 2 || s[0] != "Basic" { + return http.StatusUnauthorized + } + b, err := base64.StdEncoding.DecodeString(s[1]) + if err != nil { + return http.StatusUnauthorized + } + pair := strings.SplitN(string(b), ":", 2) + if len(pair) != 2 { + return http.StatusUnauthorized + } + + if pair[1] != password { + return http.StatusUnauthorized + } + return http.StatusOK +} + func provision(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) defer r.Body.Close() @@ -69,8 +90,7 @@ func provision(w http.ResponseWriter, r *http.Request) { return } - apiResp, err := apiInsert(c, instanceName, tier) - if err == instanceExists { + if _, err := apiInsert(c, instanceName, tier); err == instanceExists { http.Error(w, "App is already provisioned", http.StatusConflict) return } else if err != nil { @@ -82,7 +102,7 @@ func provision(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(herokuResponse{ ID: instanceName, Config: herokuResponseConfig{ - InstanceURL: apiResp.IPAddresses[0].IPAddress, + InstanceURL: "TEST", }, Message: "Provision successful!", }) @@ -108,7 +128,7 @@ func apiInsert(c appengine.Context, instanceName, tier string) (*apiResponse, er Tier: tier, ActivationPolicy: "ON_DEMAND", AuthorizedGAEApplications: []string{appengine.AppID(c)}, - PricingPlan: "PACKAGE", + PricingPlan: "PER_USE", ReplicationType: "ASYNCHRONOUS", }, }) @@ -163,5 +183,5 @@ type herokuResponse struct { } type herokuResponseConfig struct { - InstanceURL string `json:"INSTANCE_URL"` + InstanceURL string `json:"GOOGLECLOUDSQL_URL"` }