func processProvision(reqData *messages.RequestData, req *messages.Request, role string) ([]byte, error) { log.Println("received provisioning request") box, secret, err := crypto.ProvisionSecret(reqData.Length, reqData.Ident, role) if err != nil { log.Printf("failed to provision secret (%v)", err) return messages.NewServerResponse(false, "Failed to provision secret.", nil) } tx, err := db.Begin() if err != nil { log.Printf("initiating database transaction failed (%v)", err) return messages.NewServerResponse(false, "Failed to provision secret.", nil) } err = db.StoreSecret(tx, box, reqData.Ident, role, req.Public) db.Finalise(tx, err) if err != nil { log.Printf("failed to store secret (%v)", err) return messages.NewServerResponse(false, "Failed to provision secret.", nil) } var response = struct { Secret []byte `json:"secret"` }{secret} out, err := json.Marshal(response) if err != nil { log.Printf("failed to marshal json response (%v)", err) return messages.NewServerResponse(false, "Failed to provision secret.", nil) } return messages.NewServerResponse(true, "Secret provisioned.", out) }
func processStore(reqData *messages.RequestData, req *messages.Request, role string) ([]byte, error) { box, err := crypto.SecureSecret(reqData.Secret, reqData.Ident, role) if err != nil { return messages.NewServerResponse(false, "Failed to store secret.", nil) } tx, err := db.Begin() if err != nil { log.Printf("initiating database transaction failed (%v)", err) return messages.NewServerResponse(false, "Failed to store secret.", nil) } err = db.StoreSecret(tx, box, reqData.Ident, role, req.Public) db.Finalise(tx, err) if err != nil { return messages.NewServerResponse(false, "Failed to provision secret.", nil) } return messages.NewServerResponse(true, "Stored secret.", nil) }