github.com/argoproj/argo-cd@v1.8.7/server/gpgkey/gpgkey.go (about)

     1  package gpgkey
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	gpgkeypkg "github.com/argoproj/argo-cd/pkg/apiclient/gpgkey"
    10  	appsv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    11  	"github.com/argoproj/argo-cd/reposerver/apiclient"
    12  	"github.com/argoproj/argo-cd/server/rbacpolicy"
    13  	"github.com/argoproj/argo-cd/util/db"
    14  	"github.com/argoproj/argo-cd/util/gpg"
    15  	"github.com/argoproj/argo-cd/util/rbac"
    16  )
    17  
    18  // Server provides a service of type GPGKeyService
    19  type Server struct {
    20  	db            db.ArgoDB
    21  	repoClientset apiclient.Clientset
    22  	enf           *rbac.Enforcer
    23  }
    24  
    25  // NewServer returns a new instance of the service with type GPGKeyService
    26  func NewServer(
    27  	repoClientset apiclient.Clientset,
    28  	db db.ArgoDB,
    29  	enf *rbac.Enforcer,
    30  ) *Server {
    31  	return &Server{
    32  		db:            db,
    33  		repoClientset: repoClientset,
    34  		enf:           enf,
    35  	}
    36  }
    37  
    38  // ListGnuPGPublicKeys returns a list of GnuPG public keys in the configuration
    39  func (s *Server) List(ctx context.Context, q *gpgkeypkg.GnuPGPublicKeyQuery) (*appsv1.GnuPGPublicKeyList, error) {
    40  	if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceGPGKeys, rbacpolicy.ActionGet, ""); err != nil {
    41  		return nil, err
    42  	}
    43  	keys, err := s.db.ListConfiguredGPGPublicKeys(ctx)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	keyList := &appsv1.GnuPGPublicKeyList{}
    48  	for _, v := range keys {
    49  		// Remove key's data from list result to save some bytes
    50  		v.KeyData = ""
    51  		keyList.Items = append(keyList.Items, *v)
    52  	}
    53  	return keyList, nil
    54  }
    55  
    56  // GetGnuPGPublicKey retrieves a single GPG public key from the configuration
    57  func (s *Server) Get(ctx context.Context, q *gpgkeypkg.GnuPGPublicKeyQuery) (*appsv1.GnuPGPublicKey, error) {
    58  	if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceGPGKeys, rbacpolicy.ActionGet, ""); err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	keyID := gpg.KeyID(q.KeyID)
    63  	if keyID == "" {
    64  		return nil, fmt.Errorf("KeyID is malformed or empty")
    65  	}
    66  
    67  	keys, err := s.db.ListConfiguredGPGPublicKeys(ctx)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	if key, ok := keys[keyID]; ok {
    73  		return key, nil
    74  	}
    75  
    76  	return nil, fmt.Errorf("No such key: %s", keyID)
    77  }
    78  
    79  // CreateGnuPGPublicKey adds one or more GPG public keys to the server's configuration
    80  func (s *Server) Create(ctx context.Context, q *gpgkeypkg.GnuPGPublicKeyCreateRequest) (*gpgkeypkg.GnuPGPublicKeyCreateResponse, error) {
    81  	if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceGPGKeys, rbacpolicy.ActionCreate, ""); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	keyData := strings.TrimSpace(q.Publickey.KeyData)
    86  	if keyData == "" {
    87  		return nil, fmt.Errorf("Submitted key data is empty")
    88  	}
    89  
    90  	added, skipped, err := s.db.AddGPGPublicKey(ctx, q.Publickey.KeyData)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	items := make([]appsv1.GnuPGPublicKey, 0)
    96  	for _, k := range added {
    97  		items = append(items, *k)
    98  	}
    99  
   100  	response := &gpgkeypkg.GnuPGPublicKeyCreateResponse{
   101  		Created: &appsv1.GnuPGPublicKeyList{Items: items},
   102  		Skipped: skipped,
   103  	}
   104  
   105  	return response, nil
   106  }
   107  
   108  // DeleteGnuPGPublicKey removes a single GPG public key from the server's configuration
   109  func (s *Server) Delete(ctx context.Context, q *gpgkeypkg.GnuPGPublicKeyQuery) (*gpgkeypkg.GnuPGPublicKeyResponse, error) {
   110  	if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceGPGKeys, rbacpolicy.ActionDelete, ""); err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	err := s.db.DeleteGPGPublicKey(ctx, q.KeyID)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return &gpgkeypkg.GnuPGPublicKeyResponse{}, nil
   120  }