github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/operators/uidgidresolver/uidgidresolver.go (about)

     1  // Copyright 2024 The Inspektor Gadget 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 uidgidresolver provides an operator that enriches events by looking
    16  // up uid and gid resolving them to the corresponding username and groupname.
    17  // Only /etc/passwd and /etc/group is read on the host. Therefore the name for a
    18  // corresponding id could be wrong.
    19  package uidgidresolver
    20  
    21  import (
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    23  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    24  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    25  )
    26  
    27  const (
    28  	OperatorName = "UidGidResolver"
    29  )
    30  
    31  type UidResolverInterface interface {
    32  	GetUid() uint32
    33  	SetUserName(string)
    34  }
    35  
    36  type GidResolverInterface interface {
    37  	GetGid() uint32
    38  	SetGroupName(string)
    39  }
    40  
    41  type UidGidResolver struct{}
    42  
    43  func (k *UidGidResolver) Name() string {
    44  	return OperatorName
    45  }
    46  
    47  func (k *UidGidResolver) Description() string {
    48  	return "UidGidResolver resolves uid and gid to username and groupname"
    49  }
    50  
    51  func (k *UidGidResolver) GlobalParamDescs() params.ParamDescs {
    52  	return nil
    53  }
    54  
    55  func (k *UidGidResolver) ParamDescs() params.ParamDescs {
    56  	return nil
    57  }
    58  
    59  func (k *UidGidResolver) Dependencies() []string {
    60  	return nil
    61  }
    62  
    63  func (k *UidGidResolver) CanOperateOn(gadget gadgets.GadgetDesc) bool {
    64  	_, hasUidResolverInterface := gadget.EventPrototype().(UidResolverInterface)
    65  	_, hasGidResolverInterface := gadget.EventPrototype().(GidResolverInterface)
    66  	return hasUidResolverInterface || hasGidResolverInterface
    67  }
    68  
    69  func (k *UidGidResolver) Init(params *params.Params) error {
    70  	return nil
    71  }
    72  
    73  func (k *UidGidResolver) Close() error {
    74  	return nil
    75  }
    76  
    77  func (k *UidGidResolver) Instantiate(gadgetCtx operators.GadgetContext, gadgetInstance any, params *params.Params) (operators.OperatorInstance, error) {
    78  	uidGidCache := GetUserGroupCache()
    79  
    80  	return &UidGidResolverInstance{
    81  		gadgetCtx:      gadgetCtx,
    82  		gadgetInstance: gadgetInstance,
    83  		uidGidCache:    uidGidCache,
    84  	}, nil
    85  }
    86  
    87  type UidGidResolverInstance struct {
    88  	gadgetCtx      operators.GadgetContext
    89  	gadgetInstance any
    90  	uidGidCache    UserGroupCache
    91  }
    92  
    93  func (m *UidGidResolverInstance) Name() string {
    94  	return "UidGidResolverInstance"
    95  }
    96  
    97  func (m *UidGidResolverInstance) PreGadgetRun() error {
    98  	return m.uidGidCache.Start()
    99  }
   100  
   101  func (m *UidGidResolverInstance) PostGadgetRun() error {
   102  	m.uidGidCache.Stop()
   103  	return nil
   104  }
   105  
   106  func (m *UidGidResolverInstance) enrich(ev any) {
   107  	uidResolver := ev.(UidResolverInterface)
   108  	if uidResolver != nil {
   109  		uid := uidResolver.GetUid()
   110  		uidResolver.SetUserName(m.uidGidCache.GetUsername(uid))
   111  	}
   112  
   113  	gidResolver := ev.(GidResolverInterface)
   114  	if gidResolver != nil {
   115  		gid := gidResolver.GetGid()
   116  		gidResolver.SetGroupName(m.uidGidCache.GetGroupname(gid))
   117  	}
   118  }
   119  
   120  func (m *UidGidResolverInstance) EnrichEvent(ev any) error {
   121  	m.enrich(ev)
   122  	return nil
   123  }
   124  
   125  func init() {
   126  	operators.Register(&UidGidResolver{})
   127  }