github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/containers/podman/container.go (about)

     1  // Copyright 2025 Google LLC
     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  //go:build linux
    16  
    17  package podman
    18  
    19  import (
    20  	"net"
    21  	"time"
    22  )
    23  
    24  // container struct contains the podman container metadata
    25  type container struct {
    26  	config *containerConfig
    27  	state  *containerState
    28  }
    29  
    30  // containerConfig is a subset of the Podman's actual containerConfig
    31  type containerConfig struct {
    32  	// embedded sub-configs
    33  	containerRootFSConfig
    34  	containerNetworkConfig
    35  
    36  	ID           string `json:"id"`
    37  	Pod          string `json:"pod,omitempty"`
    38  	Namespace    string `json:"namespace,omitempty"`
    39  	RawImageName string `json:"RawImageName,omitempty"`
    40  }
    41  
    42  // containerRootFSConfig contains the info about podman's Rootfs
    43  type containerRootFSConfig struct {
    44  	RootfsImageID string `json:"rootfsImageID,omitempty"`
    45  }
    46  
    47  // containerNetworkConfig contains info about podman's containerNetworkConfig
    48  type containerNetworkConfig struct {
    49  	StaticIP     net.IP              `json:"staticIP,omitempty"`
    50  	ExposedPorts map[uint16][]string `json:"exposedPorts,omitempty"`
    51  }
    52  
    53  // containerState contains info about podman's containers state
    54  type containerState struct {
    55  	State        containerStatus `json:"state"`
    56  	StartedTime  time.Time       `json:"startedTime"`
    57  	FinishedTime time.Time       `json:"finishedTime"`
    58  	ExitCode     int32           `json:"exitCode,omitempty"`
    59  	Exited       bool            `json:"exited,omitempty"`
    60  	PID          int             `json:"pid,omitempty"`
    61  }
    62  
    63  // containerStatus represents the current state of a container
    64  type containerStatus int
    65  
    66  // The numbers in the enums correspond to the integer values from the sqliteDB/boltDB
    67  const (
    68  	// containerStateUnknown indicates that the container is in an error
    69  	// state where information about it cannot be retrieved
    70  	containerStateUnknown containerStatus = 0
    71  	// containerStateConfigured indicates that the container has had its
    72  	// storage configured but it has not been created in the OCI runtime
    73  	containerStateConfigured containerStatus = 1
    74  	// containerStateCreated indicates the container has been created in
    75  	// the OCI runtime but not started
    76  	containerStateCreated containerStatus = 2
    77  	// containerStateRunning indicates the container is currently executing
    78  	containerStateRunning containerStatus = 3
    79  	// containerStateStopped indicates that the container was running but has
    80  	// exited
    81  	containerStateStopped containerStatus = 4
    82  	// containerStatePaused indicates that the container has been paused
    83  	containerStatePaused containerStatus = 5
    84  	// containerStateExited indicates the container has stopped and been
    85  	// cleaned up
    86  	containerStateExited containerStatus = 6
    87  	// containerStateRemoving indicates the container is in the process of
    88  	// being removed.
    89  	containerStateRemoving containerStatus = 7
    90  	// containerStateStopping indicates the container is in the process of
    91  	// being stopped.
    92  	containerStateStopping containerStatus = 8
    93  )
    94  
    95  // String returns a string representation for users of a container state.
    96  func (t containerStatus) String() string {
    97  	switch t {
    98  	case containerStateUnknown:
    99  		return "unknown"
   100  	case containerStateConfigured:
   101  		// The naming here is confusing, but it's necessary for Docker
   102  		// compatibility - their Created state is our Configured state.
   103  		return "created"
   104  	case containerStateCreated:
   105  		// Docker does not have an equivalent to this state, so give it
   106  		// a clear name. Most of the time this is a purely transitory
   107  		// state between Configured and Running so we don't expect to
   108  		// see it much anyways.
   109  		return "initialized"
   110  	case containerStateRunning:
   111  		return "running"
   112  	case containerStateStopped:
   113  		return "stopped"
   114  	case containerStatePaused:
   115  		return "paused"
   116  	case containerStateExited:
   117  		return "exited"
   118  	case containerStateRemoving:
   119  		return "removing"
   120  	case containerStateStopping:
   121  		return "stopping"
   122  	}
   123  	return "bad state"
   124  }