Skip to content
Snippets Groups Projects
revoke.go 3.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
    Copyright (c) 2018 Vereign AG [https://www.vereign.com]
    
    This is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    */
    
    package handler
    
    import (
    	"code.vereign.com/code/viam-apis/data-storage-agent/client"
    	"code.vereign.com/code/viam-apis/key-storage-agent/api"
    	"code.vereign.com/code/viam-apis/utils"
    	"code.vereign.com/code/viam-apis/versions"
    	"golang.org/x/net/context"
    )
    
    func (s *KeyStorageServerImpl) Revoke(ctx context.Context, in *api.RevokeRequest) (*api.RevokeResponse, error) {
    	revokeResponse := &api.RevokeResponse{}
    
    
    	revokeResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Revoked = true })
    
    	if revokeResponse.StatusList != nil {
    		return revokeResponse, nil
    	}
    
    	revokeResponse.StatusList = utils.AddStatus(revokeResponse.StatusList, "200", api.StatusType_INFO, "Keys revoked")
    	return revokeResponse, nil
    }
    
    func (s *KeyStorageServerImpl) Suspend(ctx context.Context, in *api.SuspendRequest) (*api.SuspendResponse, error) {
    	suspendResponse := &api.SuspendResponse{}
    
    	suspendResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = true })
    	if suspendResponse.StatusList != nil {
    		return suspendResponse, nil
    
    	suspendResponse.StatusList = utils.AddStatus(suspendResponse.StatusList, "200", api.StatusType_INFO, "Keys suspended")
    	return suspendResponse, nil
    }
    
    func (s *KeyStorageServerImpl) Resume(ctx context.Context, in *api.ResumeRequest) (*api.ResumeResponse, error) {
    	resumeResponse := &api.ResumeResponse{}
    
    	resumeResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = false })
    	if resumeResponse.StatusList != nil {
    		return resumeResponse, nil
    
    	resumeResponse.StatusList = utils.AddStatus(resumeResponse.StatusList, "200", api.StatusType_INFO, "Keys resumed")
    	return resumeResponse, nil
    }
    
    func (s *KeyStorageServerImpl) Rename(ctx context.Context, in *api.RenameRequest) (*api.RenameResponse, error) {
    	renameResponse := &api.RenameResponse{}
    
    	renameResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Name = in.Name })
    	if renameResponse.StatusList != nil {
    		return renameResponse, nil
    	}
    	renameResponse.StatusList = utils.AddStatus(renameResponse.StatusList, "200", api.StatusType_INFO, "Keys renamed")
    	return renameResponse, nil
    
    func (s *KeyStorageServerImpl) updateAll(ctx context.Context, uuid string, update func(*api.Key)) []*api.Status {
    	auth := s.CreateAuthentication(ctx)
    
    	client := &client.DataStorageClientImpl{}
    	client.SetUpClient(auth, s.DataStorageUrl, s.CertPEM, s.KeyPEM, s.CaCertPEM, s.MaxMessageSize)
    	defer client.CloseClient()
    
    	for _, kType := range []api.KeyType{api.KeyType_PRIVATE, api.KeyType_PUBLIC, api.KeyType_CERTIFICATE} {
    		statusList := updateKey(client, uuid, kType, update)
    		if statusList != nil {
    			return statusList
    		}
    	}
    
    	return nil
    }
    
    func updateKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType, update func(*api.Key)) []*api.Status {
    
    	key, statusList := getKey(client, uuid, keyType)
    	if statusList != nil {
    
    	update(key)
    
    
    	_, errors, err := client.DoPutDataCall("keys", uuid+"/"+api.KeyType.String(keyType), key, versions.EntitiesManagementAgentApiVersion)
    	statusList = handlePutDataErrors(statusList, errors, err)
    	if statusList != nil && len(statusList) > 0 {
    		return statusList
    	}
    
    	return nil
    }