golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/relui/sign/sign.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sign
     6  
     7  import (
     8  	"context"
     9  
    10  	"golang.org/x/build/internal/relui/protos"
    11  )
    12  
    13  // Service is an interface for a release artifact signing service.
    14  //
    15  // Each call blocks until either the request has been acknowledged or the passed in context has been canceled.
    16  // Setting a timeout on the context is recommended.
    17  type Service interface {
    18  	// SignArtifact creates a request to sign a release artifact.
    19  	// The object URI must be URIs for file(s) on the service private GCS.
    20  	SignArtifact(ctx context.Context, bt BuildType, objectURI []string) (jobID string, _ error)
    21  	// ArtifactSigningStatus retrieves the status of an existing signing request,
    22  	// or an error indicating that the status couldn't be determined.
    23  	// If the status is completed, objectURI will be populated with the URIs for signed files in GCS.
    24  	ArtifactSigningStatus(ctx context.Context, jobID string) (_ Status, description string, objectURI []string, _ error)
    25  	// CancelSigning marks a previous signing request as no longer needed,
    26  	// possibly allowing resources to be freed sooner than otherwise.
    27  	CancelSigning(ctx context.Context, jobID string) error
    28  }
    29  
    30  // Status of the signing request.
    31  type Status int
    32  
    33  const (
    34  	StatusUnknown Status = iota
    35  	StatusRunning
    36  	StatusFailed
    37  	StatusCompleted
    38  	StatusNotFound
    39  )
    40  
    41  // String is the string representation for the signing request status.
    42  func (bs Status) String() string {
    43  	switch bs {
    44  	case StatusRunning:
    45  		return "Running"
    46  	case StatusFailed:
    47  		return "Failed"
    48  	case StatusCompleted:
    49  		return "Completed"
    50  	case StatusNotFound:
    51  		return "NotFound"
    52  	default:
    53  		return "Unknown"
    54  	}
    55  }
    56  
    57  // BuildType is the type of build the signing request is for.
    58  type BuildType int
    59  
    60  const (
    61  	BuildUnspecified BuildType = iota
    62  
    63  	BuildMacOS
    64  	BuildWindows
    65  	BuildGPG
    66  
    67  	BuildMacOSConstructInstallerOnly
    68  	BuildWindowsConstructInstallerOnly
    69  
    70  	BuildMacOSBinary
    71  	BuildWindowsBinary
    72  )
    73  
    74  // proto is the corresponding protobuf definition for the signing request build type.
    75  func (bt BuildType) proto() protos.SignArtifactRequest_BuildType {
    76  	switch bt {
    77  	case BuildMacOS:
    78  		return protos.SignArtifactRequest_BUILD_TYPE_MACOS
    79  	case BuildWindows:
    80  		return protos.SignArtifactRequest_BUILD_TYPE_WINDOWS
    81  	case BuildGPG:
    82  		return protos.SignArtifactRequest_BUILD_TYPE_GPG
    83  	case BuildMacOSConstructInstallerOnly:
    84  		return protos.SignArtifactRequest_BUILD_TYPE_MACOS_CONSTRUCT_INSTALLER_ONLY
    85  	case BuildWindowsConstructInstallerOnly:
    86  		return protos.SignArtifactRequest_BUILD_TYPE_WINDOWS_CONSTRUCT_INSTALLER_ONLY
    87  	case BuildMacOSBinary:
    88  		return protos.SignArtifactRequest_BUILD_TYPE_MACOS_BINARY
    89  	case BuildWindowsBinary:
    90  		return protos.SignArtifactRequest_BUILD_TYPE_WINDOWS_BINARY
    91  	default:
    92  		return protos.SignArtifactRequest_BUILD_TYPE_UNSPECIFIED
    93  	}
    94  }
    95  
    96  func (bt BuildType) String() string {
    97  	switch bt {
    98  	case BuildMacOS:
    99  		return "macOS"
   100  	case BuildWindows:
   101  		return "Windows"
   102  	case BuildGPG:
   103  		return "GPG"
   104  	case BuildMacOSConstructInstallerOnly:
   105  		return "macOS (construct installer only)"
   106  	case BuildWindowsConstructInstallerOnly:
   107  		return "Windows (construct installer only)"
   108  	case BuildMacOSBinary:
   109  		return "macOS (binary)"
   110  	case BuildWindowsBinary:
   111  		return "Windows (binary)"
   112  	default:
   113  		return "unspecified"
   114  	}
   115  }