github.com/secure-build/gitlab-runner@v12.5.0+incompatible/common/network.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	"gitlab.com/gitlab-org/gitlab-runner/helpers/url"
     9  )
    10  
    11  type UpdateState int
    12  type UploadState int
    13  type DownloadState int
    14  type JobState string
    15  type JobFailureReason string
    16  
    17  const (
    18  	Pending JobState = "pending"
    19  	Running JobState = "running"
    20  	Failed  JobState = "failed"
    21  	Success JobState = "success"
    22  )
    23  
    24  const (
    25  	NoneFailure         JobFailureReason = ""
    26  	ScriptFailure       JobFailureReason = "script_failure"
    27  	RunnerSystemFailure JobFailureReason = "runner_system_failure"
    28  	JobExecutionTimeout JobFailureReason = "job_execution_timeout"
    29  )
    30  
    31  const (
    32  	UpdateSucceeded UpdateState = iota
    33  	UpdateNotFound
    34  	UpdateAbort
    35  	UpdateFailed
    36  	UpdateRangeMismatch
    37  )
    38  
    39  const (
    40  	UploadSucceeded UploadState = iota
    41  	UploadTooLarge
    42  	UploadForbidden
    43  	UploadFailed
    44  )
    45  
    46  const (
    47  	DownloadSucceeded DownloadState = iota
    48  	DownloadForbidden
    49  	DownloadFailed
    50  	DownloadNotFound
    51  )
    52  
    53  type FeaturesInfo struct {
    54  	Variables               bool `json:"variables"`
    55  	Image                   bool `json:"image"`
    56  	Services                bool `json:"services"`
    57  	Artifacts               bool `json:"artifacts"`
    58  	Cache                   bool `json:"cache"`
    59  	Shared                  bool `json:"shared"`
    60  	UploadMultipleArtifacts bool `json:"upload_multiple_artifacts"`
    61  	UploadRawArtifacts      bool `json:"upload_raw_artifacts"`
    62  	Session                 bool `json:"session"`
    63  	Terminal                bool `json:"terminal"`
    64  	Refspecs                bool `json:"refspecs"`
    65  	Masking                 bool `json:"masking"`
    66  	Proxy                   bool `json:"proxy"`
    67  }
    68  
    69  type RegisterRunnerParameters struct {
    70  	Description    string `json:"description,omitempty"`
    71  	Tags           string `json:"tag_list,omitempty"`
    72  	RunUntagged    bool   `json:"run_untagged"`
    73  	Locked         bool   `json:"locked"`
    74  	AccessLevel    string `json:"access_level,omitempty"`
    75  	MaximumTimeout int    `json:"maximum_timeout,omitempty"`
    76  	Active         bool   `json:"active"`
    77  }
    78  
    79  type RegisterRunnerRequest struct {
    80  	RegisterRunnerParameters
    81  	Info  VersionInfo `json:"info,omitempty"`
    82  	Token string      `json:"token,omitempty"`
    83  }
    84  
    85  type RegisterRunnerResponse struct {
    86  	Token string `json:"token,omitempty"`
    87  }
    88  
    89  type VerifyRunnerRequest struct {
    90  	Token string `json:"token,omitempty"`
    91  }
    92  
    93  type UnregisterRunnerRequest struct {
    94  	Token string `json:"token,omitempty"`
    95  }
    96  
    97  type VersionInfo struct {
    98  	Name         string       `json:"name,omitempty"`
    99  	Version      string       `json:"version,omitempty"`
   100  	Revision     string       `json:"revision,omitempty"`
   101  	Platform     string       `json:"platform,omitempty"`
   102  	Architecture string       `json:"architecture,omitempty"`
   103  	Executor     string       `json:"executor,omitempty"`
   104  	Shell        string       `json:"shell,omitempty"`
   105  	Features     FeaturesInfo `json:"features"`
   106  }
   107  
   108  type JobRequest struct {
   109  	Info       VersionInfo  `json:"info,omitempty"`
   110  	Token      string       `json:"token,omitempty"`
   111  	LastUpdate string       `json:"last_update,omitempty"`
   112  	Session    *SessionInfo `json:"session,omitempty"`
   113  }
   114  
   115  type SessionInfo struct {
   116  	URL           string `json:"url,omitempty"`
   117  	Certificate   string `json:"certificate,omitempty"`
   118  	Authorization string `json:"authorization,omitempty"`
   119  }
   120  
   121  type JobInfo struct {
   122  	Name        string `json:"name"`
   123  	Stage       string `json:"stage"`
   124  	ProjectID   int    `json:"project_id"`
   125  	ProjectName string `json:"project_name"`
   126  }
   127  
   128  type GitInfoRefType string
   129  
   130  const (
   131  	RefTypeBranch GitInfoRefType = "branch"
   132  	RefTypeTag    GitInfoRefType = "tag"
   133  )
   134  
   135  type GitInfo struct {
   136  	RepoURL   string         `json:"repo_url"`
   137  	Ref       string         `json:"ref"`
   138  	Sha       string         `json:"sha"`
   139  	BeforeSha string         `json:"before_sha"`
   140  	RefType   GitInfoRefType `json:"ref_type"`
   141  	Refspecs  []string       `json:"refspecs"`
   142  	Depth     int            `json:"depth"`
   143  }
   144  
   145  type RunnerInfo struct {
   146  	Timeout int `json:"timeout"`
   147  }
   148  
   149  type StepScript []string
   150  
   151  type StepName string
   152  
   153  const (
   154  	StepNameScript      StepName = "script"
   155  	StepNameAfterScript StepName = "after_script"
   156  )
   157  
   158  type StepWhen string
   159  
   160  const (
   161  	StepWhenOnFailure StepWhen = "on_failure"
   162  	StepWhenOnSuccess StepWhen = "on_success"
   163  	StepWhenAlways    StepWhen = "always"
   164  )
   165  
   166  type CachePolicy string
   167  
   168  const (
   169  	CachePolicyUndefined CachePolicy = ""
   170  	CachePolicyPullPush  CachePolicy = "pull-push"
   171  	CachePolicyPull      CachePolicy = "pull"
   172  	CachePolicyPush      CachePolicy = "push"
   173  )
   174  
   175  type Step struct {
   176  	Name         StepName   `json:"name"`
   177  	Script       StepScript `json:"script"`
   178  	Timeout      int        `json:"timeout"`
   179  	When         StepWhen   `json:"when"`
   180  	AllowFailure bool       `json:"allow_failure"`
   181  }
   182  
   183  type Steps []Step
   184  
   185  type Image struct {
   186  	Name       string   `json:"name"`
   187  	Alias      string   `json:"alias,omitempty"`
   188  	Command    []string `json:"command,omitempty"`
   189  	Entrypoint []string `json:"entrypoint,omitempty"`
   190  	Ports      []Port   `json:"ports,omitempty"`
   191  }
   192  
   193  type Port struct {
   194  	Number   int    `json:"number,omitempty"`
   195  	Protocol string `json:"protocol,omitempty"`
   196  	Name     string `json:"name,omitempty"`
   197  }
   198  
   199  type Services []Image
   200  
   201  type ArtifactPaths []string
   202  
   203  type ArtifactWhen string
   204  
   205  const (
   206  	ArtifactWhenOnFailure ArtifactWhen = "on_failure"
   207  	ArtifactWhenOnSuccess ArtifactWhen = "on_success"
   208  	ArtifactWhenAlways    ArtifactWhen = "always"
   209  )
   210  
   211  func (when ArtifactWhen) OnSuccess() bool {
   212  	return when == "" || when == ArtifactWhenOnSuccess || when == ArtifactWhenAlways
   213  }
   214  
   215  func (when ArtifactWhen) OnFailure() bool {
   216  	return when == ArtifactWhenOnFailure || when == ArtifactWhenAlways
   217  }
   218  
   219  type ArtifactFormat string
   220  
   221  const (
   222  	ArtifactFormatDefault ArtifactFormat = ""
   223  	ArtifactFormatZip     ArtifactFormat = "zip"
   224  	ArtifactFormatGzip    ArtifactFormat = "gzip"
   225  	ArtifactFormatRaw     ArtifactFormat = "raw"
   226  )
   227  
   228  type Artifact struct {
   229  	Name      string         `json:"name"`
   230  	Untracked bool           `json:"untracked"`
   231  	Paths     ArtifactPaths  `json:"paths"`
   232  	When      ArtifactWhen   `json:"when"`
   233  	Type      string         `json:"artifact_type"`
   234  	Format    ArtifactFormat `json:"artifact_format"`
   235  	ExpireIn  string         `json:"expire_in"`
   236  }
   237  
   238  type Artifacts []Artifact
   239  
   240  type Cache struct {
   241  	Key       string        `json:"key"`
   242  	Untracked bool          `json:"untracked"`
   243  	Policy    CachePolicy   `json:"policy"`
   244  	Paths     ArtifactPaths `json:"paths"`
   245  }
   246  
   247  func (c Cache) CheckPolicy(wanted CachePolicy) (bool, error) {
   248  	switch c.Policy {
   249  	case CachePolicyUndefined, CachePolicyPullPush:
   250  		return true, nil
   251  	case CachePolicyPull, CachePolicyPush:
   252  		return wanted == c.Policy, nil
   253  	}
   254  
   255  	return false, fmt.Errorf("Unknown cache policy %s", c.Policy)
   256  }
   257  
   258  type Caches []Cache
   259  
   260  type Credentials struct {
   261  	Type     string `json:"type"`
   262  	URL      string `json:"url"`
   263  	Username string `json:"username"`
   264  	Password string `json:"password"`
   265  }
   266  
   267  type DependencyArtifactsFile struct {
   268  	Filename string `json:"filename"`
   269  	Size     int64  `json:"size"`
   270  }
   271  
   272  type Dependency struct {
   273  	ID            int                     `json:"id"`
   274  	Token         string                  `json:"token"`
   275  	Name          string                  `json:"name"`
   276  	ArtifactsFile DependencyArtifactsFile `json:"artifacts_file"`
   277  }
   278  
   279  type Dependencies []Dependency
   280  
   281  type GitlabFeatures struct {
   282  	TraceSections bool `json:"trace_sections"`
   283  }
   284  
   285  type JobResponse struct {
   286  	ID            int            `json:"id"`
   287  	Token         string         `json:"token"`
   288  	AllowGitFetch bool           `json:"allow_git_fetch"`
   289  	JobInfo       JobInfo        `json:"job_info"`
   290  	GitInfo       GitInfo        `json:"git_info"`
   291  	RunnerInfo    RunnerInfo     `json:"runner_info"`
   292  	Variables     JobVariables   `json:"variables"`
   293  	Steps         Steps          `json:"steps"`
   294  	Image         Image          `json:"image"`
   295  	Services      Services       `json:"services"`
   296  	Artifacts     Artifacts      `json:"artifacts"`
   297  	Cache         Caches         `json:"cache"`
   298  	Credentials   []Credentials  `json:"credentials"`
   299  	Dependencies  Dependencies   `json:"dependencies"`
   300  	Features      GitlabFeatures `json:"features"`
   301  
   302  	TLSCAChain  string `json:"-"`
   303  	TLSAuthCert string `json:"-"`
   304  	TLSAuthKey  string `json:"-"`
   305  }
   306  
   307  func (j *JobResponse) RepoCleanURL() string {
   308  	return url_helpers.CleanURL(j.GitInfo.RepoURL)
   309  }
   310  
   311  type UpdateJobRequest struct {
   312  	Info          VersionInfo      `json:"info,omitempty"`
   313  	Token         string           `json:"token,omitempty"`
   314  	State         JobState         `json:"state,omitempty"`
   315  	FailureReason JobFailureReason `json:"failure_reason,omitempty"`
   316  }
   317  
   318  type JobCredentials struct {
   319  	ID          int    `long:"id" env:"CI_JOB_ID" description:"The build ID to upload artifacts for"`
   320  	Token       string `long:"token" env:"CI_JOB_TOKEN" required:"true" description:"Build token"`
   321  	URL         string `long:"url" env:"CI_SERVER_URL" required:"true" description:"GitLab CI URL"`
   322  	TLSCAFile   string `long:"tls-ca-file" env:"CI_SERVER_TLS_CA_FILE" description:"File containing the certificates to verify the peer when using HTTPS"`
   323  	TLSCertFile string `long:"tls-cert-file" env:"CI_SERVER_TLS_CERT_FILE" description:"File containing certificate for TLS client auth with runner when using HTTPS"`
   324  	TLSKeyFile  string `long:"tls-key-file" env:"CI_SERVER_TLS_KEY_FILE" description:"File containing private key for TLS client auth with runner when using HTTPS"`
   325  }
   326  
   327  func (j *JobCredentials) GetURL() string {
   328  	return j.URL
   329  }
   330  
   331  func (j *JobCredentials) GetTLSCAFile() string {
   332  	return j.TLSCAFile
   333  }
   334  
   335  func (j *JobCredentials) GetTLSCertFile() string {
   336  	return j.TLSCertFile
   337  }
   338  
   339  func (j *JobCredentials) GetTLSKeyFile() string {
   340  	return j.TLSKeyFile
   341  }
   342  
   343  func (j *JobCredentials) GetToken() string {
   344  	return j.Token
   345  }
   346  
   347  type UpdateJobInfo struct {
   348  	ID            int
   349  	State         JobState
   350  	FailureReason JobFailureReason
   351  }
   352  
   353  type ArtifactsOptions struct {
   354  	BaseName string
   355  	ExpireIn string
   356  	Format   ArtifactFormat
   357  	Type     string
   358  }
   359  
   360  type FailuresCollector interface {
   361  	RecordFailure(reason JobFailureReason, runnerDescription string)
   362  }
   363  
   364  type JobTrace interface {
   365  	io.Writer
   366  	Success()
   367  	Fail(err error, failureReason JobFailureReason)
   368  	SetCancelFunc(cancelFunc context.CancelFunc)
   369  	SetFailuresCollector(fc FailuresCollector)
   370  	SetMasked(values []string)
   371  	IsStdout() bool
   372  }
   373  
   374  type Network interface {
   375  	RegisterRunner(config RunnerCredentials, parameters RegisterRunnerParameters) *RegisterRunnerResponse
   376  	VerifyRunner(config RunnerCredentials) bool
   377  	UnregisterRunner(config RunnerCredentials) bool
   378  	RequestJob(config RunnerConfig, sessionInfo *SessionInfo) (*JobResponse, bool)
   379  	UpdateJob(config RunnerConfig, jobCredentials *JobCredentials, jobInfo UpdateJobInfo) UpdateState
   380  	PatchTrace(config RunnerConfig, jobCredentials *JobCredentials, content []byte, startOffset int) (int, UpdateState)
   381  	DownloadArtifacts(config JobCredentials, artifactsFile string) DownloadState
   382  	UploadRawArtifacts(config JobCredentials, reader io.Reader, options ArtifactsOptions) UploadState
   383  	ProcessJob(config RunnerConfig, buildCredentials *JobCredentials) (JobTrace, error)
   384  }