client

package module
v0.0.0-...-3f02e16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2023 License: Apache-2.0 Imports: 26 Imported by: 0

README

harbor-client

Go Report Card

Golang client of goharbor/harbor.

NOT STABLE YET.

Features

  • OCI Distribution Client Supported,see oci.go.
  • Light && Simple
  • Avoid import additional libraries from harbor, like beego etc.
  • Compatible with harbor v2
  • auto

Install

go get github.com/oneinstack/harbor-client

Example

harbor client:

import client "github.com/oneinstack/harbor-client"

cli, _ := client.NewClient("harbor.example.com", client.WithBasicAuth("admin", "password"))

image := "harbor.example.com/library/nginx:alpine"
project, repository, reference, _ := client.ParseHarborSuitImage(image)

ctx:=context.Background()
artifact, _ := cli.GetArtifact(ctx, project, repository, reference, client.GetArtifactOptions{})

fmt.Println(artifact)

Ommited error handle

OCI distribution client:

ocicli, _ := client.NewOCIDistributionClient("registry.example.com", client.BasicAuth("user", "password"))

if err := ocicli.Ping(context.Background()); err != nil {
    log.Infof("may be auth failed")
    return
}

tags, _ := ocicli.ListTags(context.Background(), "library/nginx")
fmt.Printf("tags: %s", tags.Tags)

Documents

See Go Doc

Contributing

Everyone is welcome to contribute.no limit, just creeate a Merge Request.

See Project for more information.

Documentation

Index

Examples

Constants

View Source
const (
	AdditionBuildHistory    = "build_history"
	AdditionHelmValues      = "values.yaml"
	AdditionHelmReadme      = "readme.md"
	AdditionDependencies    = "dependencies"
	AdditionVulnerabilities = "vulnerabilities"
)
View Source
const (
	LabelColorRed    = "#C92100"
	LabelColorGreen  = "#00AB9A"
	LabelColorWhite  = "#FFFFFF"
	LabelColorYellow = "#FFDC0B"
)

Variables

View Source
var ErrNotHarborImage = errors.New("not a harbor suit image")

Functions

func ParseHarborSuitImage

func ParseHarborSuitImage(image string) (project, repository, reference string, err error)

ParseHarborImage parse a image and return harbor project,repository,reference

func ParseImag

func ParseImag(image string) (domain, path, name, tag string, err error)

ParseImag barbor.foo.com/project/artifact:tag -> barbor.foo.com,project,artifact,tag barbor.foo.com/project/foo/artifact:tag -> barbor.foo.com,project,foo/artifact,tag barbor.foo.com/artifact:tag -> barbor.foo.com,library,artifact,tag project/artifact:tag -> docker.io,project,artifact,tag

Types

type Addition

type Addition string
type AdditionLink struct {
	HREF     string `json:"href"`
	Absolute bool   `json:"absolute"` // specify the href is an absolute URL or not
}

AdditionLink is a link via that the addition can be fetched

type Artifact

type Artifact struct {
	artifact.Artifact
	Tags          []Tag                               `json:"tags"`           // the list of tags that attached to the artifact
	AdditionLinks map[string]AdditionLink             `json:"addition_links"` // the resource link for build history(image), values.yaml(chart), dependency(chart), etc
	Labels        []Label                             `json:"labels"`
	ScanOverview  map[string]vuln.NativeReportSummary `json:"scan_overview"`
}

* Redefined AdditionLink,Tag,Label * To avoid import lots of useless dependencies from harbor like beego,borm etc..

type Auth

type Auth func(req *http.Request)

func BasicAuth

func BasicAuth(username, password string) Auth

func TokenAuth

func TokenAuth(token string) Auth

type Client

type Client struct {
	Server string
	Auth   Auth
	// contains filtered or unexported fields
}

func NewClient

func NewClient(addr string, options ...Option) (*Client, error)
Example
package main

import (
	"context"
	"fmt"
	"log"

	client "github.com/oneinstack/harbor-client"
)

func main() {
	cli, err := client.NewClient("harbor.example.com", client.WithBasicAuth("admin", "password"))
	if err != nil {
		log.Fatal(err)
	}
	sysinfo, err := cli.SystemInfo(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(sysinfo)

	image := "harbor.example.com/library/nginx:alpine"
	project, repository, reference, err := client.ParseHarborSuitImage(image)
	if err != nil {
		log.Fatal(err)
	}
	artifact, err := cli.GetArtifact(context.Background(), project, repository, reference, client.GetArtifactOptions{
		WithScanOverview: true,
		WithLabel:        true,
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(artifact)
}
Output:

func (*Client) AddArtifactLabelFromKey

func (c *Client) AddArtifactLabelFromKey(ctx context.Context, project, repository, reference string, key, desc string, color LabelColor) error

AddArtifactLabelFromKey adds a label to artifact by the key. it find key name from all keys if key not exist,create and do it again

func (*Client) AttachArtifactLabel

func (c *Client) AttachArtifactLabel(ctx context.Context, project, repository, reference string, labelID int64) error

POST /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/labels body: {"id":2}

func (*Client) AttachChartLabel

func (c *Client) AttachChartLabel(ctx context.Context, repo, name, version string, labelID int64) error

POST /chartrepo/{repo}/charts/{name}/{version}/labels body: {"id":2}

func (*Client) CopyArtifact

func (c *Client) CopyArtifact(ctx context.Context, project, repository, from string) (CopyArtifactResponse, error)

POST /projects/{project_name}/repositories/{repository_name}/artifacts from: The artifact from which the new artifact is copied from, the format should be "project/repository:tag" or "project/repository@digest".

func (*Client) CreateArtifactTag

func (c *Client) CreateArtifactTag(ctx context.Context, project, repository, reference string, tag Tag) error

POST /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags

func (*Client) CreateGlobalLabel

func (c *Client) CreateGlobalLabel(ctx context.Context, key, desc string, color LabelColor) error

POST /labels

func (*Client) CreateProject

func (c *Client) CreateProject(ctx context.Context, project projectmodels.Project) error

POST /projects

func (*Client) CreateProjectLabel

func (c *Client) CreateProjectLabel(ctx context.Context, projectID int64, key, desc string, color LabelColor) error

POST /labels

func (*Client) DeleteArtifact

func (c *Client) DeleteArtifact(ctx context.Context, project, repository, reference string) error

DELETE /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}

func (*Client) DeleteArtifactTag

func (c *Client) DeleteArtifactTag(ctx context.Context, project, repository, reference, tag string) error

DELETE /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags/{tag_name}

func (*Client) DeleteLabel

func (c *Client) DeleteLabel(ctx context.Context, id int) error

DELETE /labels/{id}

func (*Client) DeleteProject

func (c *Client) DeleteProject(ctx context.Context, projectID int) error

DELETE /projects/{project_id}

func (*Client) DeleteRepository

func (c *Client) DeleteRepository(ctx context.Context, project, repository string) error

DeleteRepository DELETE /projects/{project_name}/repositories/{repository_name}

func (*Client) DettachArtifactLabel

func (c *Client) DettachArtifactLabel(ctx context.Context, project, repository, reference string, labelID int) error

DELETE /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/labels/{label_id}

func (*Client) DettachArtifactLabelFromKey

func (c *Client) DettachArtifactLabelFromKey(ctx context.Context, project, repository, reference string, key string) error

func (*Client) DettachChartLabel

func (c *Client) DettachChartLabel(ctx context.Context, repo, name, version string, labelID int64) ([]model.Label, error)

Delete /chartrepo/{repo}/charts/{name}/{version}/labels

func (*Client) GetArtifact

func (c *Client) GetArtifact(ctx context.Context, project, repository, reference string, options GetArtifactOptions) (*Artifact, error)

GET /repositories/{{repository_name}}/artifacts/{{reference}}?

func (*Client) GetArtifactadditionDependencies

func (c *Client) GetArtifactadditionDependencies(ctx context.Context, project, repository, reference string) ([]chart.Dependency, error)

func (*Client) GetArtifactadditionVulnerabilities

func (c *Client) GetArtifactadditionVulnerabilities(ctx context.Context, project, repository, reference string) (Vulnerabilities, error)

func (*Client) GetArtifactadditions

func (c *Client) GetArtifactadditions(ctx context.Context, project, repository, reference string, addition Addition) ([]byte, error)

GET /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/additions/{addition}

func (*Client) GetIcon

func (c *Client) GetIcon(ctx context.Context, digest string) (Icon, error)

GET /icons/{digest}

func (*Client) GetLabel

func (c *Client) GetLabel(ctx context.Context, id int) (model.Label, error)

GET /labels/{id}

func (*Client) GetProject

func (c *Client) GetProject(ctx context.Context, projectID int) (projectmodels.Project, error)

GET /projects/{project_id}

func (*Client) GetProjectDeletable

func (c *Client) GetProjectDeletable(ctx context.Context, projectID int) error

GET /projects/{project_id}/_deletable

func (*Client) GetProjectLogs

func (c *Client) GetProjectLogs(ctx context.Context, project string, options CommonListOptions) error

GET /projects/{project_name}/logs

func (*Client) GetProjectSummary

func (c *Client) GetProjectSummary(ctx context.Context, projectID int) (apilib.ProjectSummary, error)

GET /projects/{project_id}/summary

func (*Client) GetRepository

func (c *Client) GetRepository(ctx context.Context, project, repository string) (Repository, error)

GetRepositories GET /projects/{project_name}/repositories/{repository_name}

func (*Client) GetScanReportLog

func (c *Client) GetScanReportLog(ctx context.Context, project, repository, reference string, reportID int) ([]byte, error)

GET /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/scan/{report_id}/log

func (*Client) HeadProject

func (c *Client) HeadProject(ctx context.Context, project string) error

HEAD /projects/{project_id}

func (*Client) ListArtifactTags

func (c *Client) ListArtifactTags(ctx context.Context, project, repository, reference string, options ListTagsOptions) ([]Tag, error)

GET /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/tags

func (*Client) ListArtifacts

func (c *Client) ListArtifacts(ctx context.Context, project, repository string, options ListArtifactsOptions) ([]Artifact, error)

GET /projects/{project_name}/repositories/{repository_name}/artifacts

func (*Client) ListAuditLogs

func (c *Client) ListAuditLogs(ctx context.Context, options CommonListOptions) ([]model.AuditLog, error)

GET /audit-logs

func (*Client) ListChartLabels

func (c *Client) ListChartLabels(ctx context.Context, repo, name, version string) ([]model.Label, error)

GET /chartrepo/{repo}/charts/{name}/{version}/labels

func (*Client) ListGlobalLabels

func (c *Client) ListGlobalLabels(ctx context.Context) ([]model.Label, error)

GET /labels?scope=g

func (*Client) ListProjectLabels

func (c *Client) ListProjectLabels(ctx context.Context, projectID int) ([]model.Label, error)

GET /labels?scope=p&project_id={id}

func (*Client) ListProjects

func (c *Client) ListProjects(ctx context.Context, options ListProjectsOptions) error

GET /projects

func (*Client) ListRepositories

func (c *Client) ListRepositories(ctx context.Context, project string, options RepositoriesListOptions) (RepositoryList, error)

ListRepositories GET /projects/{project_name}/repositories

func (*Client) OIDCPing

func (c *Client) OIDCPing(ctx context.Context) (OIDCPing, error)

GET /system/oidc/ping

func (*Client) ScanArtifact

func (c *Client) ScanArtifact(ctx context.Context, project, repository, reference string) error

POST /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/scan

func (*Client) SystemInfo

func (c *Client) SystemInfo(ctx context.Context) (systeminfo.Data, error)

SystemInfo GET /systeminfo

func (*Client) UpdateLabel

func (c *Client) UpdateLabel(ctx context.Context, label model.Label) error

PUT /labels/{id}

func (*Client) UpdateProject

func (c *Client) UpdateProject(ctx context.Context, projectID int, project projectmodels.Project) error

PUT /projects/{project_id}

func (*Client) UpdateRepository

func (c *Client) UpdateRepository(ctx context.Context, project string, repository Repository) error

PutRepository PUT /projects/{project_name}/repositories/{repository_name}

type CommonListOptions

type CommonListOptions struct {
	// The page number
	// Default value: 1
	Page int `json:"page"`

	// The size of per page
	// Default value: 10
	Size int `json:"size"`
	// Query string to query resources.
	// Supported query patterns are "exact match(k=v)", "fuzzy match(k=~v)","range(k=[min~max])",
	// "list with union releationship(k={v1 v2 v3})" and"list with intersetion relationship(k=(v1 v2 v3))".
	// The value of range and list can be string(enclosed by " or '),integer or time(in format "2020-04-09 02:36:00").
	// All of these query patterns should be put in the query string "q=xxx" and splitted by ",". e.g. q=k1=v1,k2=~v2,k3=[min~max]
	Q string `json:"q"`
}

type CopyArtifactResponse

type CopyArtifactResponse struct {
	Location string `json:"location"` // The location of the resource
}

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

Error same as https://github.com/goharbor/harbor/blob/4e1f6633afb824cd16341044a0e82f4f1f230cd2/src/lib/errors/errors.go#L32

type GetArtifactOptions

type GetArtifactOptions struct {
	WithTag             bool
	WithScanOverview    bool
	WithLabel           bool
	WithImmutableStatus bool
	WithSignature       bool
}

type Icon

type Icon struct {
	ContentType string `json:"content_type,omitempty"`
	Content     string `json:"content,omitempty"` // base64 encoded
}

https://github.com/goharbor/harbor/blob/4e1f6633afb824cd16341044a0e82f4f1f230cd2/src/controller/icon/controller.go#L51

type Label

type Label struct {
	ID           int64     `orm:"pk;auto;column(id)" json:"id"`
	Name         string    `orm:"column(name)" json:"name"`
	Description  string    `orm:"column(description)" json:"description"`
	Color        string    `orm:"column(color)" json:"color"`
	Level        string    `orm:"column(level)" json:"-"`
	Scope        string    `orm:"column(scope)" json:"scope"`
	ProjectID    int64     `orm:"column(project_id)" json:"project_id"`
	CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
	UpdateTime   time.Time `orm:"column(update_time);auto_now" json:"update_time"`
	Deleted      bool      `orm:"column(deleted)" json:"deleted"`
}

Label holds information used for a label

type LabelColor

type LabelColor string

LabelColor must be '#xxxxxx' format

type ListArtifactsOptions

type ListArtifactsOptions struct {
	CommonListOptions
	GetArtifactOptions
}

type ListProjectsOptions

type ListProjectsOptions struct {
	CommonListOptions
	Name   string `json:"name,omitempty"`
	Public bool   `json:"public,omitempty"`
	Owner  string `json:"owner,omitempty"`
}

type ListTagsOptions

type ListTagsOptions struct {
	CommonListOptions
	WithImmutableStatus bool
	WithSignature       bool
}

type OCIDistributionClient

type OCIDistributionClient struct {
	Server string
	Auth   Auth
}

func NewOCIDistributionClient

func NewOCIDistributionClient(server string, auth Auth) (*OCIDistributionClient, error)

OCI Distribution Specification Client

For more information visit below URL https://github.com/opencontainers/distribution-spec/blob/main/spec.md#endpoints

Example
package main

import (
	"context"
	"fmt"
	"log"

	client "github.com/oneinstack/harbor-client"
)

func main() {
	ocicli, err := client.NewOCIDistributionClient("registry.example.com", client.BasicAuth("user", "password"))
	if err != nil {
		log.Fatal(err)
	}

	if err := ocicli.Ping(context.Background()); err != nil {
		log.Fatal(err)
	}

	tags, err := ocicli.ListTags(context.Background(), "library/nginx")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("tags: %s", tags.Tags)
}
Output:

func (*OCIDistributionClient) DeleteManifest

func (c *OCIDistributionClient) DeleteManifest(ctx context.Context, name, reference string) error

end-9 DELETE /v2/<name>/manifests/<reference>

func (*OCIDistributionClient) GetManifest

func (c *OCIDistributionClient) GetManifest(ctx context.Context, name, reference string) (*imagespecv1.Manifest, error)

end-3 GET/HEAD /v2/<name>/manifests/<reference>

func (*OCIDistributionClient) ListTags

end-8a GET /v2/<name>/tags/list

func (*OCIDistributionClient) ListTagsPaged

func (c *OCIDistributionClient) ListTagsPaged(ctx context.Context, name string, n, last int) (*distributionspecsv1.TagList, error)

end-8b GET /v2/<name>/tags/list?n=<integer>&last=<integer>

func (*OCIDistributionClient) Ping

see: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#determining-support To check whether or not the registry implements this specification, perform a GET request to the following endpoint: /v2/ end-1. If the response is 200 OK, then the registry implements this specification. It can used to detectd server connection and auth too. end-1 GET /v2/ 200 404/401

type OIDCPing

type OIDCPing struct {
	Url        string `json:"url,omitempty"`
	VerifyCert string `json:"verify_cert,omitempty"`
}

type Option

type Option func(opts *Client)

func WithBasicAuth

func WithBasicAuth(username, password string) Option

func WithTokenAuth

func WithTokenAuth(token string) Option

type RepositoriesListOptions

type RepositoriesListOptions struct {
	CommonListOptions
}

type Repository

type Repository struct {
	RepositoryID int64     `json:"repositoryID,omitempty"`
	Name         string    `json:"name,omitempty"`
	ProjectID    int64     `json:"projectID,omitempty"`
	Description  string    `json:"description,omitempty"`
	PullCount    int64     `json:"pullCount,omitempty"`
	StarCount    int64     `json:"starCount,omitempty"`
	CreationTime time.Time `json:"creationTime,omitempty"`
	UpdateTime   time.Time `json:"updateTime,omitempty"`
}

https://github.com/goharbor/harbor/blob/4e1f6633afb824cd16341044a0e82f4f1f230cd2/src/pkg/repository/model/model.go#L34

type RepositoryList

type RepositoryList struct {
	Total int          `json:"total,omitempty"`
	Items []Repository `json:"items,omitempty"`
	Next  string       `json:"next,omitempty"`
}

type Tag

type Tag struct {
	ID           int64     `orm:"pk;auto;column(id)" json:"id"`
	RepositoryID int64     `orm:"column(repository_id)" json:"repository_id"` // tags are the resources of repository, one repository only contains one same name tag
	ArtifactID   int64     `orm:"column(artifact_id)" json:"artifact_id"`     // the artifact ID that the tag attaches to, it changes when pushing a same name but different digest artifact
	Name         string    `orm:"column(name)" json:"name"`
	PushTime     time.Time `orm:"column(push_time)" json:"push_time"`
	PullTime     time.Time `orm:"column(pull_time)" json:"pull_time"`
	Immutable    bool      `json:"immutable"`
	Signed       bool      `json:"signed"`
}

Tag is the overall view of tag

type Vulnerabilities

type Vulnerabilities map[string]vuln.Report

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL