github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/registryv1/images.go (about) 1 package registryv1 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 8 "github.com/IBM-Cloud/bluemix-go/client" 9 "github.com/IBM-Cloud/bluemix-go/helpers" 10 "github.com/IBM-Cloud/bluemix-go/rest" 11 ) 12 13 type ImageTargetHeader struct { 14 AccountID string 15 } 16 17 //ToMap ... 18 func (c ImageTargetHeader) ToMap() map[string]string { 19 m := make(map[string]string, 1) 20 m[accountIDHeader] = c.AccountID 21 return m 22 } 23 24 //Subnets interface 25 type Images interface { 26 GetImages(params GetImageRequest, target ImageTargetHeader) (*GetImagesResponse, error) 27 InspectImage(imageName string, target ImageTargetHeader) (*ImageInspectResponse, error) 28 DeleteImage(imageName string, target ImageTargetHeader) (*DeleteImageResponse, error) 29 ImageVulnerabilities(imageName string, param ImageVulnerabilitiesRequest, target ImageTargetHeader) (*ImageVulnerabilitiesResponse, error) 30 } 31 32 type images struct { 33 client *client.Client 34 } 35 36 func newImageAPI(c *client.Client) Images { 37 return &images{ 38 client: c, 39 } 40 } 41 42 type Digesttags struct { 43 Tags map[string][]string 44 } 45 46 type Labels struct { 47 Labels map[string][]string 48 } 49 50 type GetImagesResponse []struct { 51 ID string `json:"Id"` 52 ParentID string `json:"ParentId"` 53 DigestTags map[string][]string `json:"DigestTags"` 54 RepoTags []string `json:"RepoTags"` 55 RepoDigests []string `json:"RepoDigests"` 56 Created int `json:"Created"` 57 Size int64 `json:"Size"` 58 VirtualSize int64 `json:"VirtualSize"` 59 Labels map[string]string `json:"Labels"` 60 Vulnerable string `json:"Vulnerable"` 61 VulnerabilityCount int `json:"VulnerabilityCount"` 62 ConfigurationIssueCount int `json:"ConfigurationIssueCount"` 63 IssueCount int `json:"IssueCount"` 64 ExemptIssueCount int `json:"ExemptIssueCount"` 65 } 66 type ImageInspectResponse struct { 67 ID string `json:"Id"` 68 Parent string `json:"Parent"` 69 Comment string `json:"Comment"` 70 Created time.Time `json:"Created"` 71 Container string `json:"Container"` 72 ContainerConfig struct { 73 Hostname string `json:"Hostname"` 74 Domainname string `json:"Domainname"` 75 User string `json:"User"` 76 AttachStdin bool `json:"AttachStdin"` 77 AttachStdout bool `json:"AttachStdout"` 78 AttachStderr bool `json:"AttachStderr"` 79 ExposedPorts map[string]interface{} `json:"ExposedPorts"` 80 Tty bool `json:"Tty"` 81 OpenStdin bool `json:"OpenStdin"` 82 StdinOnce bool `json:"StdinOnce"` 83 Env []string `json:"Env"` 84 Cmd []string `json:"Cmd"` 85 ArgsEscaped bool `json:"ArgsEscaped"` 86 Image string `json:"Image"` 87 Volumes map[string]interface{} `json:"Volumes"` 88 WorkingDir string `json:"WorkingDir"` 89 Entrypoint []string `json:"Entrypoint"` 90 OnBuild []string `json:"OnBuild"` 91 Labels map[string]string `json:"Labels"` 92 } `json:"ContainerConfig"` 93 DockerVersion string `json:"DockerVersion"` 94 Author string `json:"Author"` 95 Config struct { 96 Hostname string `json:"Hostname"` 97 Domainname string `json:"Domainname"` 98 User string `json:"User"` 99 AttachStdin bool `json:"AttachStdin"` 100 AttachStdout bool `json:"AttachStdout"` 101 AttachStderr bool `json:"AttachStderr"` 102 ExposedPorts map[string]interface{} `json:"ExposedPorts"` 103 Tty bool `json:"Tty"` 104 OpenStdin bool `json:"OpenStdin"` 105 StdinOnce bool `json:"StdinOnce"` 106 Env []string `json:"Env"` 107 Cmd []string `json:"Cmd"` 108 ArgsEscaped bool `json:"ArgsEscaped"` 109 Image string `json:"Image"` 110 Volumes map[string]interface{} `json:"Volumes"` 111 WorkingDir string `json:"WorkingDir"` 112 Entrypoint []string `json:"Entrypoint"` 113 OnBuild []string `json:"OnBuild"` 114 Labels map[string]string `json:"Labels"` 115 } `json:"Config"` 116 Architecture string `json:"Architecture"` 117 Os string `json:"Os"` 118 Size int64 `json:"Size"` 119 VirtualSize int64 `json:"VirtualSize"` 120 RootFS struct { 121 Type string `json:"Type"` 122 Layers []string `json:"Layers"` 123 } `json:"RootFS"` 124 } 125 126 type DeleteImageResponse struct { 127 Untagged string `json:"Untagged"` 128 } 129 130 type ImageVulnerabilitiesResponse struct { 131 Metadata struct { 132 Namespace string `json:"namespace"` 133 Complete bool `json:"complete"` 134 CrawledTime time.Time `json:"crawled_time"` 135 OsSupported bool `json:"os_supported"` 136 } `json:"metadata"` 137 Summary struct { 138 Malware struct { 139 Compliant bool `json:"compliant"` 140 Reason string `json:"reason"` 141 } `json:"malware"` 142 Compliance struct { 143 ComplianceViolations int `json:"compliance_violations"` 144 Reason string `json:"reason"` 145 Compliant bool `json:"compliant"` 146 TotalComplianceRules int `json:"total_compliance_rules"` 147 ExecutionStatus string `json:"execution_status"` 148 } `json:"compliance"` 149 Secureconfig struct { 150 Misconfigured int `json:"misconfigured"` 151 CorrectOutput int `json:"correct_output"` 152 TotalOutputDocs int `json:"total_output_docs"` 153 } `json:"secureconfig"` 154 Vulnerability struct { 155 TotalPackages int `json:"total_packages"` 156 TotalUsnsForDistro int `json:"total_usns_for_distro"` 157 VulnerableUsns int `json:"vulnerable_usns"` 158 VulnerablePackages int `json:"vulnerable_packages"` 159 } `json:"vulnerability"` 160 } `json:"summary"` 161 Detail struct { 162 Compliance []struct { 163 Reason string `json:"reason"` 164 Compliant bool `json:"compliant"` 165 Description string `json:"description"` 166 PolicyMandated bool `json:"policy_mandated"` 167 } `json:"compliance"` 168 Vulnerability []struct { 169 PackageName string `json:"package_name"` 170 Vulnerabilities []struct { 171 URL string `json:"url"` 172 Cveid []string `json:"cveid"` 173 Summary string `json:"summary"` 174 } `json:"vulnerabilities"` 175 } `json:"vulnerability"` 176 } `json:"detail"` 177 } 178 179 /*GetImageRequest contains all the parameters to send to the API endpoint 180 for the image list operation typically these are written to a http.Request 181 */ 182 type GetImageRequest struct { 183 /*IncludeIBM 184 Includes IBM-provided public images in the list of images. If this option is not specified, private images are listed only. If this option is specified more than once, the last parsed setting is the setting that is used. 185 */ 186 IncludeIBM bool 187 /*IncludePrivate 188 Includes private images in the list of images. If this option is not specified, private images are listed. If this option is specified more than once, the last parsed setting is the setting that is used. 189 */ 190 IncludePrivate bool 191 /*Namespace 192 Lists images that are stored in the specified namespace only. Query multiple namespaces by specifying this option for each namespace. If this option is not specified, images from all namespaces in the specified IBM Cloud account are listed. 193 */ 194 Namespace string 195 /*Repository 196 Lists images that are stored in the specified repository, under your namespaces. Query multiple repositories by specifying this option for each repository. If this option is not specified, images from all repos are listed. 197 */ 198 Repository string 199 /*Vulnerabilities 200 Displays Vulnerability Advisor status for the listed images. If this option is specified more than once, the last parsed setting is the setting that is used. 201 */ 202 Vulnerabilities bool 203 } 204 205 type ImageVulnerabilitiesRequest struct { 206 207 /*Advisory 208 Specifies to include advisory compliance checks in the report. 209 */ 210 Advisory bool 211 /*All 212 Specifies to include all checks in the report. If not specified or false, only failing checks are returned. 213 */ 214 All bool 215 } 216 217 func DefaultGetImageRequest() *GetImageRequest { 218 return &GetImageRequest{ 219 IncludeIBM: false, 220 IncludePrivate: true, 221 Namespace: "", 222 Repository: "", 223 Vulnerabilities: true, 224 } 225 } 226 227 func DefaultImageVulnerabilitiesRequest() *ImageVulnerabilitiesRequest { 228 return &ImageVulnerabilitiesRequest{ 229 Advisory: false, 230 All: false, 231 } 232 } 233 234 func (r *images) GetImages(params GetImageRequest, target ImageTargetHeader) (*GetImagesResponse, error) { 235 236 var retVal GetImagesResponse 237 req := rest.GetRequest(helpers.GetFullURL(*r.client.Config.Endpoint, "/api/v1/images")). 238 Query("includeIBM", strconv.FormatBool(params.IncludeIBM)). 239 Query("includePrivate", strconv.FormatBool(params.IncludePrivate)). 240 Query("vulnerabilities", strconv.FormatBool(params.Vulnerabilities)) 241 if params.Namespace != "" { 242 req = req.Query("namespace", params.Namespace) 243 } 244 if params.Repository != "repository" { 245 req = req.Query("repository", params.Repository) 246 } 247 for key, value := range target.ToMap() { 248 req.Set(key, value) 249 } 250 251 _, err := r.client.SendRequest(req, &retVal) 252 if err != nil { 253 return nil, err 254 } 255 return &retVal, err 256 } 257 258 func (r *images) InspectImage(imageName string, target ImageTargetHeader) (*ImageInspectResponse, error) { 259 260 var retVal ImageInspectResponse 261 req := rest.GetRequest(helpers.GetFullURL(*r.client.Config.Endpoint, fmt.Sprintf("/api/v1/images/%s/json", imageName))) 262 263 for key, value := range target.ToMap() { 264 req.Set(key, value) 265 } 266 267 _, err := r.client.SendRequest(req, &retVal) 268 if err != nil { 269 return nil, err 270 } 271 return &retVal, err 272 } 273 274 func (r *images) DeleteImage(imageName string, target ImageTargetHeader) (*DeleteImageResponse, error) { 275 276 var retVal DeleteImageResponse 277 req := rest.DeleteRequest(helpers.GetFullURL(*r.client.Config.Endpoint, fmt.Sprintf("/api/v1/images/%s", imageName))) 278 279 for key, value := range target.ToMap() { 280 req.Set(key, value) 281 } 282 283 _, err := r.client.SendRequest(req, &retVal) 284 if err != nil { 285 return nil, err 286 } 287 return &retVal, err 288 } 289 290 func (r *images) ImageVulnerabilities(imageName string, params ImageVulnerabilitiesRequest, target ImageTargetHeader) (*ImageVulnerabilitiesResponse, error) { 291 292 var retVal ImageVulnerabilitiesResponse 293 req := rest.GetRequest(helpers.GetFullURL(*r.client.Config.Endpoint, fmt.Sprintf("/api/v1/images/%s/vulnerabilities", imageName))). 294 Query("all", strconv.FormatBool(params.All)). 295 Query("advisory", strconv.FormatBool(params.Advisory)) 296 297 for key, value := range target.ToMap() { 298 req.Set(key, value) 299 } 300 301 _, err := r.client.SendRequest(req, &retVal) 302 if err != nil { 303 return nil, err 304 } 305 return &retVal, err 306 }