github.com/looshlee/cilium@v1.6.12/daemon/identity.go (about)

     1  // Copyright 2016-2017 Authors of Cilium
     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 main
    16  
    17  import (
    18  	"github.com/cilium/cilium/api/v1/models"
    19  	. "github.com/cilium/cilium/api/v1/server/restapi/policy"
    20  	"github.com/cilium/cilium/pkg/identity"
    21  	"github.com/cilium/cilium/pkg/identity/cache"
    22  	"github.com/cilium/cilium/pkg/identity/identitymanager"
    23  	"github.com/cilium/cilium/pkg/labels"
    24  	"github.com/cilium/cilium/pkg/logging/logfields"
    25  
    26  	"github.com/go-openapi/runtime/middleware"
    27  )
    28  
    29  type getIdentity struct{}
    30  
    31  func newGetIdentityHandler(d *Daemon) GetIdentityHandler { return &getIdentity{} }
    32  
    33  func (h *getIdentity) Handle(params GetIdentityParams) middleware.Responder {
    34  	log.WithField(logfields.Params, logfields.Repr(params)).Debug("GET /identity request")
    35  
    36  	identities := []*models.Identity{}
    37  	if params.Labels == nil {
    38  		// if labels is nil, return all identities from the kvstore
    39  		// This is in response to "identity list" command
    40  		identities = cache.GetIdentities()
    41  	} else {
    42  		identity := cache.LookupIdentity(labels.NewLabelsFromModel(params.Labels))
    43  		if identity == nil {
    44  			return NewGetIdentityIDNotFound()
    45  		}
    46  
    47  		identities = append(identities, identity.GetModel())
    48  	}
    49  
    50  	return NewGetIdentityOK().WithPayload(identities)
    51  }
    52  
    53  type getIdentityID struct{}
    54  
    55  func newGetIdentityIDHandler(d *Daemon) GetIdentityIDHandler { return &getIdentityID{} }
    56  
    57  func (h *getIdentityID) Handle(params GetIdentityIDParams) middleware.Responder {
    58  	nid, err := identity.ParseNumericIdentity(params.ID)
    59  	if err != nil {
    60  		return NewGetIdentityIDBadRequest()
    61  	}
    62  
    63  	identity := cache.LookupIdentityByID(nid)
    64  	if identity == nil {
    65  		return NewGetIdentityIDNotFound()
    66  	}
    67  
    68  	return NewGetIdentityIDOK().WithPayload(identity.GetModel())
    69  }
    70  
    71  type getIdentityEndpoints struct{}
    72  
    73  func newGetIdentityEndpointsIDHandler(d *Daemon) GetIdentityEndpointsHandler {
    74  	return &getIdentityEndpoints{}
    75  }
    76  
    77  func (h *getIdentityEndpoints) Handle(params GetIdentityEndpointsParams) middleware.Responder {
    78  	log.WithField(logfields.Params, logfields.Repr(params)).Debug("GET /identity/endpoints request")
    79  
    80  	identities := identitymanager.GetIdentityModels()
    81  
    82  	return NewGetIdentityEndpointsOK().WithPayload(identities)
    83  }