|
| 1 | +// |
| 2 | +// Author:: Salim Afiune Maya (<[email protected]>) |
| 3 | +// Copyright:: Copyright 2023, Lacework Inc. |
| 4 | +// License:: Apache License, Version 2.0 |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | +package api |
| 20 | + |
| 21 | +import ( |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/lacework/go-sdk/internal/array" |
| 25 | +) |
| 26 | + |
| 27 | +// ListContainers returns a list of Active Containers from the last 7 days |
| 28 | +func (svc *EntitiesService) ListContainers() (response ContainersEntityResponse, err error) { |
| 29 | + now := time.Now().UTC() |
| 30 | + before := now.AddDate(0, 0, -7) // 7 days from ago |
| 31 | + err = svc.Search(&response, |
| 32 | + SearchFilter{ |
| 33 | + TimeFilter: &TimeFilter{ |
| 34 | + StartTime: &before, |
| 35 | + EndTime: &now, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ) |
| 39 | + return |
| 40 | +} |
| 41 | + |
| 42 | +// ListContainersWithFilters returns a list of Active Containers based on a user defined filter |
| 43 | +func (svc *EntitiesService) ListContainersWithFilters(filters SearchFilter) (response ContainersEntityResponse, err error) { |
| 44 | + err = svc.Search(&response, filters) |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +// ListAllContainers iterates over all pages to return all active container information at once |
| 49 | +func (svc *EntitiesService) ListAllContainers() (response ContainersEntityResponse, err error) { |
| 50 | + response, err = svc.ListContainers() |
| 51 | + if err != nil { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + var ( |
| 56 | + all []ContainerEntity |
| 57 | + pageOk bool |
| 58 | + ) |
| 59 | + for { |
| 60 | + all = append(all, response.Data...) |
| 61 | + |
| 62 | + pageOk, err = svc.client.NextPage(&response) |
| 63 | + if err == nil && pageOk { |
| 64 | + continue |
| 65 | + } |
| 66 | + break |
| 67 | + } |
| 68 | + |
| 69 | + response.Data = all |
| 70 | + response.ResetPaging() |
| 71 | + return |
| 72 | +} |
| 73 | + |
| 74 | +// ListAllContainersWithFilters iterates over all pages to return all active container information at once based on a user defined filter |
| 75 | +func (svc *EntitiesService) ListAllContainersWithFilters(filters SearchFilter) (response ContainersEntityResponse, err error) { |
| 76 | + response, err = svc.ListContainersWithFilters(filters) |
| 77 | + if err != nil { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + var ( |
| 82 | + all []ContainerEntity |
| 83 | + pageOk bool |
| 84 | + ) |
| 85 | + |
| 86 | + for { |
| 87 | + all = append(all, response.Data...) |
| 88 | + |
| 89 | + pageOk, err = svc.client.NextPage(&response) |
| 90 | + if err == nil && pageOk { |
| 91 | + continue |
| 92 | + } |
| 93 | + break |
| 94 | + } |
| 95 | + |
| 96 | + response.Data = all |
| 97 | + response.ResetPaging() |
| 98 | + return |
| 99 | +} |
| 100 | + |
| 101 | +type ContainersEntityResponse struct { |
| 102 | + Data []ContainerEntity `json:"data"` |
| 103 | + Paging V2Pagination `json:"paging"` |
| 104 | +} |
| 105 | + |
| 106 | +// Fulfill Pageable interface (look at api/v2.go) |
| 107 | +func (r ContainersEntityResponse) PageInfo() *V2Pagination { |
| 108 | + return &r.Paging |
| 109 | +} |
| 110 | +func (r *ContainersEntityResponse) ResetPaging() { |
| 111 | + r.Paging = V2Pagination{} |
| 112 | +} |
| 113 | + |
| 114 | +// Total returns the total number of active containers |
| 115 | +func (r *ContainersEntityResponse) Total() int { |
| 116 | + uniqMIDs := []int{} |
| 117 | + for _, container := range r.Data { |
| 118 | + if !array.ContainsInt(uniqMIDs, container.Mid) { |
| 119 | + uniqMIDs = append(uniqMIDs, container.Mid) |
| 120 | + } |
| 121 | + } |
| 122 | + return len(uniqMIDs) |
| 123 | +} |
| 124 | + |
| 125 | +// Count returns the number of active containers with the provided image ID |
| 126 | +func (r *ContainersEntityResponse) Count(imageID string) int { |
| 127 | + uniqMIDs := []int{} |
| 128 | + for _, container := range r.Data { |
| 129 | + if container.ImageID == imageID && |
| 130 | + !array.ContainsInt(uniqMIDs, container.Mid) { |
| 131 | + uniqMIDs = append(uniqMIDs, container.Mid) |
| 132 | + } |
| 133 | + } |
| 134 | + return len(uniqMIDs) |
| 135 | +} |
| 136 | + |
| 137 | +type ContainerEntity struct { |
| 138 | + ContainerName string `json:"containerName"` |
| 139 | + ImageID string `json:"imageId"` |
| 140 | + Mid int `json:"mid"` |
| 141 | + StartTime time.Time `json:"startTime"` |
| 142 | + EndTime time.Time `json:"endTime"` |
| 143 | + PodName string `json:"podName"` |
| 144 | + PropsContainer map[string]interface{} `json:"propsContainer"` |
| 145 | + Tags map[string]interface{} `json:"tags"` |
| 146 | +} |
| 147 | + |
| 148 | +type ContainerEntityProps struct { |
| 149 | + Name string `json:"NAME"` |
| 150 | + ContainerType string `json:"CONTAINER_TYPE"` |
| 151 | + ImageAuthor string `json:"IMAGE_AUTHOR"` |
| 152 | + ImageCreatedTime time.Time `json:"IMAGE_CREATED_TIME"` |
| 153 | + ImageID string `json:"IMAGE_ID"` |
| 154 | + ImageParentID string `json:"IMAGE_PARENT_ID"` |
| 155 | + ImageRepo string `json:"IMAGE_REPO"` |
| 156 | + ImageSize int64 `json:"IMAGE_SIZE"` |
| 157 | + ImageTag string `json:"IMAGE_TAG"` |
| 158 | + ImageVersion string `json:"IMAGE_VERSION"` |
| 159 | + Ipv4 string `json:"IPV4"` |
| 160 | +} |
0 commit comments