Skip to content
Snippets Groups Projects
revoke.go 2.35 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) {
    	auth := s.CreateAuthentication(ctx)
    
    	client := &client.DataStorageClientImpl{}
    
    	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
    
    	defer client.CloseClient()
    
    	revokeResponse := &api.RevokeResponse{}
    
    	revokeResponse.StatusList = revokeKey(client, in.Uuid, api.KeyType_PRIVATE)
    	if revokeResponse.StatusList != nil {
    		return revokeResponse, nil
    	}
    
    	revokeResponse.StatusList = revokeKey(client, in.Uuid, api.KeyType_PUBLIC)
    	if revokeResponse.StatusList != nil {
    		return revokeResponse, nil
    	}
    
    	revokeResponse.StatusList = revokeKey(client, in.Uuid, api.KeyType_CERTIFICATE)
    	if revokeResponse.StatusList != nil {
    		return revokeResponse, nil
    	}
    
    	revokeResponse.StatusList = utils.AddStatus(revokeResponse.StatusList, "200", api.StatusType_INFO, "Keys revoked")
    	return revokeResponse, nil
    }
    
    func revokeKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType) []*api.Status {
    
    
    	key, statusList := getKey(client, uuid, keyType)
    	if statusList != nil {
    
    		return statusList
    	}
    
    	key.Revoked = true
    
    	_, 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
    }