go.chromium.org/luci@v0.0.0-20250314024836-d9a61d0730e6/tokenserver/appengine/impl/certconfig/rpc_get_ca_status_rpc.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package certconfig
    16  
    17  import (
    18  	"context"
    19  
    20  	"google.golang.org/grpc/codes"
    21  	"google.golang.org/grpc/status"
    22  
    23  	ds "go.chromium.org/luci/gae/service/datastore"
    24  
    25  	"go.chromium.org/luci/tokenserver/api/admin/v1"
    26  	"go.chromium.org/luci/tokenserver/appengine/impl/utils"
    27  )
    28  
    29  // GetCAStatusRPC implements CertificateAuthorities.GetCAStatus RPC method.
    30  type GetCAStatusRPC struct {
    31  }
    32  
    33  // GetCAStatus returns configuration of some CA defined in the config.
    34  func (r *GetCAStatusRPC) GetCAStatus(c context.Context, req *admin.GetCAStatusRequest) (*admin.GetCAStatusResponse, error) {
    35  	// Entities to fetch.
    36  	ca := CA{CN: req.Cn}
    37  	crl := CRL{Parent: ds.KeyForObj(c, &ca)}
    38  
    39  	// Fetch them at the same revision. It is fine if CRL is not there yet. Don't
    40  	// bother doing it in parallel: GetCAStatus is used only by admins, manually.
    41  	err := ds.RunInTransaction(c, func(c context.Context) error {
    42  		if err := ds.Get(c, &ca); err != nil {
    43  			return err // can be ErrNoSuchEntity
    44  		}
    45  		if err := ds.Get(c, &crl); err != nil && err != ds.ErrNoSuchEntity {
    46  			return err // only transient errors
    47  		}
    48  		return nil
    49  	}, nil)
    50  	switch {
    51  	case err == ds.ErrNoSuchEntity:
    52  		return &admin.GetCAStatusResponse{}, nil
    53  	case err != nil:
    54  		return nil, status.Errorf(codes.Internal, "datastore error - %s", err)
    55  	}
    56  
    57  	cfgMsg, err := ca.ParseConfig()
    58  	if err != nil {
    59  		return nil, status.Errorf(codes.Internal, "broken config in the datastore - %s", err)
    60  	}
    61  
    62  	return &admin.GetCAStatusResponse{
    63  		Config:     cfgMsg,
    64  		Cert:       utils.DumpPEM(ca.Cert, "CERTIFICATE"),
    65  		Removed:    ca.Removed,
    66  		Ready:      ca.Ready,
    67  		AddedRev:   ca.AddedRev,
    68  		UpdatedRev: ca.UpdatedRev,
    69  		RemovedRev: ca.RemovedRev,
    70  		CrlStatus:  crl.GetStatusProto(),
    71  	}, nil
    72  }