From 7fd8263c2960918da4eca9731a4bcfa5ffa0d24d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 3 Jun 2011 18:42:31 -0700 Subject: [PATCH] upload public key on start --- config/dev-server-config.json | 3 ++- server/go/camlistored/sig.go | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/config/dev-server-config.json b/config/dev-server-config.json index b8dc0d6c2..8964c13ae 100644 --- a/config/dev-server-config.json +++ b/config/dev-server-config.json @@ -29,7 +29,8 @@ "handler": "jsonsign", "handlerArgs": { "secretRing": ["_env", "${CAMLI_SECRET_RING}"], - "keyId": "26F5ABDA" + "keyId": "26F5ABDA", + "publicKeyDest": "/bs/" } }, diff --git a/server/go/camlistored/sig.go b/server/go/camlistored/sig.go index 701e17625..7a0a7a05e 100644 --- a/server/go/camlistored/sig.go +++ b/server/go/camlistored/sig.go @@ -52,6 +52,10 @@ type JSONSignHandler struct { pubKeyBlobRefServeSuffix string // "camli/sha1-xxxx" pubKeyHandler http.Handler + // Where & if our public key is published + pubKeyDest blobserver.Storage + pubKeyWritten bool + entity *openpgp.Entity } @@ -67,6 +71,7 @@ func init() { } func newJsonSignFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Handler, os.Error) { + pubKeyDestPrefix := conf.OptionalString("publicKeyDest", "") h := &JSONSignHandler{ keyId: strings.ToUpper(conf.RequiredString("keyId")), secretRing: conf.OptionalString("secretRing", ""), @@ -89,6 +94,15 @@ func newJsonSignFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Hand } h.pubKeyFetcher = ms + if pubKeyDestPrefix != "" { + sto, err := ld.GetStorage(pubKeyDestPrefix) + if err != nil { + return nil, err + } + h.pubKeyDest = sto + go h.uploadPublicKey(armoredPublicKey) + } + h.pubKeyBlobRefServeSuffix = "camli/" + h.pubKeyBlobRef.String() h.pubKeyHandler = &handlers.GetHandler{ Fetcher: ms, @@ -98,6 +112,17 @@ func newJsonSignFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (http.Hand return h, nil } +func (h *JSONSignHandler) uploadPublicKey(key string) { + if h.pubKeyDest == nil { + return + } + // TODO: error check + _, err := h.pubKeyDest.ReceiveBlob(h.pubKeyBlobRef, strings.NewReader(key)) + if err != nil { + log.Printf("upload public key: %v", err) + } +} + func (h *JSONSignHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { base := req.Header.Get("X-PrefixHandler-PathBase") subPath := req.Header.Get("X-PrefixHandler-PathSuffix") @@ -166,7 +191,7 @@ func (h *JSONSignHandler) handleVerify(rw http.ResponseWriter, req *http.Request m["errorMessage"] = errStr } - rw.WriteHeader(http.StatusOK) // no HTTP response code fun, error info in JSON + rw.WriteHeader(http.StatusOK) // no HTTP response code fun, error info in JSON httputil.ReturnJson(rw, m) }