upload public key on start

This commit is contained in:
Brad Fitzpatrick 2011-06-03 18:42:31 -07:00
parent 35031e761e
commit 7fd8263c29
2 changed files with 28 additions and 2 deletions

View File

@ -29,7 +29,8 @@
"handler": "jsonsign",
"handlerArgs": {
"secretRing": ["_env", "${CAMLI_SECRET_RING}"],
"keyId": "26F5ABDA"
"keyId": "26F5ABDA",
"publicKeyDest": "/bs/"
}
},

View File

@ -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)
}