|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/letsencrypt/boulder/akamai" |
| 13 | + "github.com/letsencrypt/boulder/cmd" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + listenAddr := flag.String("listen", "localhost:6789", "Address to listen on") |
| 18 | + secret := flag.String("secret", "", "Akamai client secret") |
| 19 | + flag.Parse() |
| 20 | + |
| 21 | + // v2 |
| 22 | + v2Purges := [][]string{} |
| 23 | + v3Purges := [][]string{} |
| 24 | + mu := sync.Mutex{} |
| 25 | + |
| 26 | + http.HandleFunc("/debug/get-purges", func(w http.ResponseWriter, r *http.Request) { |
| 27 | + mu.Lock() |
| 28 | + defer mu.Unlock() |
| 29 | + body, err := json.Marshal(struct { |
| 30 | + V2 [][]string |
| 31 | + V3 [][]string |
| 32 | + }{V2: v2Purges, V3: v3Purges}) |
| 33 | + if err != nil { |
| 34 | + w.WriteHeader(http.StatusInternalServerError) |
| 35 | + return |
| 36 | + } |
| 37 | + w.Write(body) |
| 38 | + return |
| 39 | + }) |
| 40 | + |
| 41 | + http.HandleFunc("/debug/reset-purges", func(w http.ResponseWriter, r *http.Request) { |
| 42 | + mu.Lock() |
| 43 | + defer mu.Unlock() |
| 44 | + v2Purges, v3Purges = [][]string{}, [][]string{} |
| 45 | + w.WriteHeader(http.StatusOK) |
| 46 | + return |
| 47 | + }) |
| 48 | + |
| 49 | + // Since v2 and v3 APIs share a bunch of logic just mash them into a single |
| 50 | + // handler. |
| 51 | + http.HandleFunc("/ccu/", func(w http.ResponseWriter, r *http.Request) { |
| 52 | + if r.Method != http.MethodPost { |
| 53 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 54 | + fmt.Println("Wrong method:", r.Method) |
| 55 | + return |
| 56 | + } |
| 57 | + mu.Lock() |
| 58 | + defer mu.Unlock() |
| 59 | + var purgeRequest struct { |
| 60 | + Objects []string `json:"objects"` |
| 61 | + Type string `json:"type"` |
| 62 | + Action string `json:"action"` |
| 63 | + } |
| 64 | + body, err := ioutil.ReadAll(r.Body) |
| 65 | + if err != nil { |
| 66 | + w.WriteHeader(http.StatusBadRequest) |
| 67 | + fmt.Println("Can't read body:", err) |
| 68 | + return |
| 69 | + } |
| 70 | + if err = akamai.CheckSignature(*secret, "http://"+*listenAddr, r, body); err != nil { |
| 71 | + w.WriteHeader(http.StatusUnauthorized) |
| 72 | + fmt.Println("Bad signature:", err) |
| 73 | + return |
| 74 | + } |
| 75 | + if err = json.Unmarshal(body, &purgeRequest); err != nil { |
| 76 | + w.WriteHeader(http.StatusBadRequest) |
| 77 | + fmt.Println("Can't unmarshal:", err) |
| 78 | + return |
| 79 | + } |
| 80 | + if r.URL.Path == "/ccu/v2/queues/default" { |
| 81 | + if purgeRequest.Type != "arl" || purgeRequest.Action != "remove" || len(purgeRequest.Objects) == 0 { |
| 82 | + w.WriteHeader(http.StatusBadRequest) |
| 83 | + fmt.Println("Bad parameters:", purgeRequest) |
| 84 | + return |
| 85 | + } |
| 86 | + v2Purges = append(v2Purges, purgeRequest.Objects) |
| 87 | + } else if r.URL.Path == "/ccu/v3/delete/url/staging" { |
| 88 | + if len(purgeRequest.Objects) == 0 || purgeRequest.Type != "" || purgeRequest.Action != "" { |
| 89 | + w.WriteHeader(http.StatusBadRequest) |
| 90 | + fmt.Println("Bad parameters:", purgeRequest) |
| 91 | + return |
| 92 | + } |
| 93 | + v3Purges = append(v3Purges, purgeRequest.Objects) |
| 94 | + } |
| 95 | + |
| 96 | + respObj := struct { |
| 97 | + PurgeID string |
| 98 | + HTTPStatus int |
| 99 | + EstimatedSeconds int |
| 100 | + }{ |
| 101 | + PurgeID: "welcome-to-the-purge", |
| 102 | + HTTPStatus: http.StatusCreated, |
| 103 | + EstimatedSeconds: 153, |
| 104 | + } |
| 105 | + w.WriteHeader(http.StatusCreated) |
| 106 | + resp, err := json.Marshal(respObj) |
| 107 | + if err != nil { |
| 108 | + return |
| 109 | + } |
| 110 | + w.Write(resp) |
| 111 | + }) |
| 112 | + |
| 113 | + go log.Fatal(http.ListenAndServe(*listenAddr, nil)) |
| 114 | + cmd.CatchSignals(nil, nil) |
| 115 | +} |
0 commit comments