Example #1
0
// NewStorageAuthorityServer constructs an RPC server
func NewStorageAuthorityServer(rpc Server, impl core.StorageAuthority) error {
	rpc.Handle(MethodUpdateRegistration, func(ctx context.Context, req []byte) (response []byte, err error) {
		var reg core.Registration
		if err = json.Unmarshal(req, &reg); err != nil {
			improperMessage(MethodUpdateRegistration, err, req)
			return
		}

		err = impl.UpdateRegistration(ctx, reg)
		return
	})

	rpc.Handle(MethodGetRegistration, func(ctx context.Context, req []byte) (response []byte, err error) {
		var grReq getRegistrationRequest
		err = json.Unmarshal(req, &grReq)
		if err != nil {
			improperMessage(MethodGetRegistration, err, req)
			return
		}

		reg, err := impl.GetRegistration(ctx, grReq.ID)
		if err != nil {
			return
		}

		response, err = json.Marshal(reg)
		if err != nil {
			errorCondition(MethodGetRegistration, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodGetRegistrationByKey, func(ctx context.Context, req []byte) (response []byte, err error) {
		var jwk *jose.JsonWebKey
		if err = json.Unmarshal(req, &jwk); err != nil {
			improperMessage(MethodGetRegistrationByKey, err, req)
			return
		}

		reg, err := impl.GetRegistrationByKey(ctx, jwk)
		if err != nil {
			return
		}

		response, err = json.Marshal(reg)
		if err != nil {
			errorCondition(MethodGetRegistrationByKey, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodGetAuthorization, func(ctx context.Context, req []byte) (response []byte, err error) {
		authz, err := impl.GetAuthorization(ctx, string(req))
		if err != nil {
			return
		}

		response, err = json.Marshal(authz)
		if err != nil {
			errorCondition(MethodGetAuthorization, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodGetValidAuthorizations, func(ctx context.Context, req []byte) (response []byte, err error) {
		var mreq getValidAuthorizationsRequest
		if err = json.Unmarshal(req, &mreq); err != nil {
			improperMessage(MethodGetValidAuthorizations, err, req)
			return
		}

		auths, err := impl.GetValidAuthorizations(ctx, mreq.RegID, mreq.Names, mreq.Now)
		if err != nil {
			return
		}

		response, err = json.Marshal(auths)
		if err != nil {
			errorCondition(MethodGetValidAuthorizations, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodAddCertificate, func(ctx context.Context, req []byte) (response []byte, err error) {
		var acReq addCertificateRequest
		err = json.Unmarshal(req, &acReq)
		if err != nil {
			improperMessage(MethodAddCertificate, err, req)
			return
		}

		id, err := impl.AddCertificate(ctx, acReq.Bytes, acReq.RegID)
		if err != nil {
			return
		}
		response = []byte(id)
		return
	})

	rpc.Handle(MethodNewRegistration, func(ctx context.Context, req []byte) (response []byte, err error) {
		var registration core.Registration
		err = json.Unmarshal(req, &registration)
		if err != nil {
			improperMessage(MethodNewRegistration, err, req)
			return
		}

		output, err := impl.NewRegistration(ctx, registration)
		if err != nil {
			return
		}

		response, err = json.Marshal(output)
		if err != nil {
			errorCondition(MethodNewRegistration, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodNewPendingAuthorization, func(ctx context.Context, req []byte) (response []byte, err error) {
		var authz core.Authorization
		if err = json.Unmarshal(req, &authz); err != nil {
			improperMessage(MethodNewPendingAuthorization, err, req)
			return
		}

		output, err := impl.NewPendingAuthorization(ctx, authz)
		if err != nil {
			return
		}

		response, err = json.Marshal(output)
		if err != nil {
			errorCondition(MethodNewPendingAuthorization, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodUpdatePendingAuthorization, func(ctx context.Context, req []byte) (response []byte, err error) {
		var authz core.Authorization
		if err = json.Unmarshal(req, &authz); err != nil {
			improperMessage(MethodUpdatePendingAuthorization, err, req)
			return
		}

		err = impl.UpdatePendingAuthorization(ctx, authz)
		return
	})

	rpc.Handle(MethodFinalizeAuthorization, func(ctx context.Context, req []byte) (response []byte, err error) {
		var authz core.Authorization
		if err = json.Unmarshal(req, &authz); err != nil {
			improperMessage(MethodFinalizeAuthorization, err, req)
			return
		}

		err = impl.FinalizeAuthorization(ctx, authz)
		return
	})

	rpc.Handle(MethodRevokeAuthorizationsByDomain, func(ctx context.Context, req []byte) (response []byte, err error) {
		var reqObj revokeAuthsRequest
		err = json.Unmarshal(req, &reqObj)
		if err != nil {
			return
		}
		aRevoked, paRevoked, err := impl.RevokeAuthorizationsByDomain(ctx, reqObj.Ident)
		if err != nil {
			return
		}
		var raResp = revokeAuthsResponse{FinalRevoked: aRevoked, PendingRevoked: paRevoked}
		response, err = json.Marshal(raResp)
		return
	})

	rpc.Handle(MethodGetCertificate, func(ctx context.Context, req []byte) (response []byte, err error) {
		cert, err := impl.GetCertificate(ctx, string(req))
		if err != nil {
			return
		}

		jsonResponse, err := json.Marshal(cert)
		if err != nil {
			errorCondition(MethodGetCertificate, err, req)
			return
		}

		return jsonResponse, nil
	})

	rpc.Handle(MethodGetCertificateStatus, func(ctx context.Context, req []byte) (response []byte, err error) {
		status, err := impl.GetCertificateStatus(ctx, string(req))
		if err != nil {
			return
		}

		response, err = json.Marshal(status)
		if err != nil {
			errorCondition(MethodGetCertificateStatus, err, req)
			return
		}
		return
	})

	rpc.Handle(MethodMarkCertificateRevoked, func(ctx context.Context, req []byte) (response []byte, err error) {
		var mcrReq markCertificateRevokedRequest

		if err = json.Unmarshal(req, &mcrReq); err != nil {
			improperMessage(MethodMarkCertificateRevoked, err, req)
			return
		}

		err = impl.MarkCertificateRevoked(ctx, mcrReq.Serial, mcrReq.ReasonCode)
		return
	})

	rpc.Handle(MethodCountCertificatesRange, func(ctx context.Context, req []byte) (response []byte, err error) {
		var cReq countRequest
		err = json.Unmarshal(req, &cReq)
		if err != nil {
			return
		}

		count, err := impl.CountCertificatesRange(ctx, cReq.Start, cReq.End)
		if err != nil {
			return
		}
		return json.Marshal(count)
	})

	rpc.Handle(MethodCountCertificatesByNames, func(ctx context.Context, req []byte) (response []byte, err error) {
		var cReq countCertificatesByNamesRequest
		err = json.Unmarshal(req, &cReq)
		if err != nil {
			return
		}

		counts, err := impl.CountCertificatesByNames(ctx, cReq.Names, cReq.Earliest, cReq.Latest)
		if err != nil {
			return
		}
		return json.Marshal(counts)
	})

	rpc.Handle(MethodCountRegistrationsByIP, func(ctx context.Context, req []byte) (response []byte, err error) {
		var cReq countRegistrationsByIPRequest
		err = json.Unmarshal(req, &cReq)
		if err != nil {
			return
		}

		count, err := impl.CountRegistrationsByIP(ctx, cReq.IP, cReq.Earliest, cReq.Latest)
		if err != nil {
			return
		}
		return json.Marshal(count)
	})

	rpc.Handle(MethodCountPendingAuthorizations, func(ctx context.Context, req []byte) (response []byte, err error) {
		var cReq countPendingAuthorizationsRequest
		err = json.Unmarshal(req, &cReq)
		if err != nil {
			return
		}

		count, err := impl.CountPendingAuthorizations(ctx, cReq.RegID)
		if err != nil {
			return
		}
		return json.Marshal(count)
	})

	rpc.Handle(MethodGetSCTReceipt, func(ctx context.Context, req []byte) (response []byte, err error) {
		var gsctReq struct {
			Serial string
			LogID  string
		}

		err = json.Unmarshal(req, &gsctReq)
		if err != nil {
			improperMessage(MethodGetSCTReceipt, err, req)
			return
		}

		sct, err := impl.GetSCTReceipt(ctx, gsctReq.Serial, gsctReq.LogID)
		jsonResponse, err := json.Marshal(sct)
		if err != nil {
			errorCondition(MethodGetSCTReceipt, err, req)
			return
		}

		return jsonResponse, nil
	})

	rpc.Handle(MethodAddSCTReceipt, func(ctx context.Context, req []byte) (response []byte, err error) {
		var sct core.SignedCertificateTimestamp
		err = json.Unmarshal(req, &sct)
		if err != nil {
			improperMessage(MethodAddSCTReceipt, err, req)
			return
		}

		return nil, impl.AddSCTReceipt(ctx, core.SignedCertificateTimestamp(sct))
	})

	rpc.Handle(MethodCountFQDNSets, func(ctx context.Context, req []byte) (response []byte, err error) {
		var r countFQDNsRequest
		err = json.Unmarshal(req, &r)
		if err != nil {
			errorCondition(MethodCountFQDNSets, err, req)
			return
		}
		count, err := impl.CountFQDNSets(ctx, r.Window, r.Names)
		if err != nil {
			return
		}

		response, err = json.Marshal(countFQDNSetsResponse{count})
		if err != nil {
			errorCondition(MethodCountFQDNSets, err, req)
			return
		}

		return
	})

	rpc.Handle(MethodFQDNSetExists, func(ctx context.Context, req []byte) (response []byte, err error) {
		var r fqdnSetExistsRequest
		err = json.Unmarshal(req, &r)
		if err != nil {
			errorCondition(MethodFQDNSetExists, err, req)
			return
		}
		exists, err := impl.FQDNSetExists(ctx, r.Names)
		if err != nil {
			return
		}
		response, err = json.Marshal(fqdnSetExistsResponse{exists})
		if err != nil {
			errorCondition(MethodFQDNSetExists, err, req)
			return
		}

		return
	})

	rpc.Handle(MethodDeactivateAuthorizationSA, func(ctx context.Context, req []byte) (response []byte, err error) {
		err = impl.DeactivateAuthorization(ctx, string(req))
		return
	})

	rpc.Handle(MethodDeactivateRegistrationSA, func(ctx context.Context, req []byte) (response []byte, err error) {
		var drReq deactivateRegistrationRequest
		err = json.Unmarshal(req, &drReq)
		if err != nil {
			return
		}
		err = impl.DeactivateRegistration(ctx, drReq.ID)
		return
	})

	return nil
}