Verified Commit e2966c82 authored by Saahil Sabu Hameed's avatar Saahil Sabu Hameed Committed by GitLab
Browse files

feat(attestations): add ListSigningCertificates

Changelog: Improvements
parent c65d798c
Loading
Loading
Loading
Loading
+37
Changes for attestations.go: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import (
var (
	routeProjectsIDAttestationsID                  = route("projects/%s/attestations/%s")
	routeProjectsIDAttestationsIDDownload          = route("projects/%s/attestations/%d/download")
	routeProjectsIDAttestationsSigningCertificates = route("projects/%s/attestations/signing_certificates")
)

type (
@@ -22,6 +23,13 @@ type (
		//
		// GitLab API docs: https://docs.gitlab.com/api/attestations/#download-an-attestation
		DownloadAttestation(pid any, attestationIID int64, options ...RequestOptionFunc) ([]byte, *Response, error)

		// ListSigningCertificates gets a list of the unexpired certificate
		// authority (CA) certificates of a project, active and inactive.
		// GitLab uses these CAs for the LOCAL_CA attestation method.
		//
		// GitLab API docs: https://docs.gitlab.com/api/attestations/#list-all-signing-certificates
		ListSigningCertificates(pid any, opt *ListSigningCertificatesOptions, options ...RequestOptionFunc) ([]*SigningCertificate, *Response, error)
	}

	// AttestationsService handles communication with the keys related methods
@@ -51,6 +59,18 @@ type Attestation struct {
	DownloadURL   string     `json:"download_url"`
}

// SigningCertificate represents a certificate authority (CA) certificate
// that GitLab uses for the LOCAL_CA attestation method.
//
// GitLab API docs: https://docs.gitlab.com/api/attestations/#list-all-signing-certificates
type SigningCertificate struct {
	ID          int64      `json:"id"`
	Certificate string     `json:"certificate"`
	Active      bool       `json:"active"`
	ExpiresAt   *time.Time `json:"expires_at"`
	CreatedAt   *time.Time `json:"created_at"`
}

func (s *AttestationsService) ListAttestations(pid any, subjectDigest string, options ...RequestOptionFunc) ([]*Attestation, *Response, error) {
	return do[[]*Attestation](s.client,
		withMethod(http.MethodGet),
@@ -68,3 +88,20 @@ func (s *AttestationsService) DownloadAttestation(pid any, attestationIID int64,

	return b.Bytes(), resp, err
}

// ListSigningCertificatesOptions represents the available
// ListSigningCertificates() options.
//
// GitLab API docs: https://docs.gitlab.com/api/attestations/#list-all-signing-certificates
type ListSigningCertificatesOptions struct {
	ListOptions
}

func (s *AttestationsService) ListSigningCertificates(pid any, opt *ListSigningCertificatesOptions, options ...RequestOptionFunc) ([]*SigningCertificate, *Response, error) {
	return do[[]*SigningCertificate](s.client,
		withMethod(http.MethodGet),
		withPath(routeProjectsIDAttestationsSigningCertificates, ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}
+54
Changes for attestations_test.go: 54 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestListAttestations(t *testing.T) {
@@ -74,3 +75,56 @@ func TestDownloadAttestation(t *testing.T) {

	assert.Equal(t, []byte(expectedOut), outBytes)
}

func TestListSigningCertificates(t *testing.T) {
	t.Parallel()

	// GIVEN a project with an inactive and an active signing certificate
	mux, client := setup(t)
	mux.HandleFunc("/api/v4/projects/1337/attestations/signing_certificates", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		testURL(t, r, "/api/v4/projects/1337/attestations/signing_certificates?page=1&per_page=2")
		fmt.Fprint(w, `[
		{
			"id": 1,
			"certificate": "-----BEGIN CERTIFICATE-----\nMIIBjTCCATSgAwIBAgIUVlaC...\n-----END CERTIFICATE-----\n",
			"active": false,
			"expires_at": "2036-09-15T10:00:00.000Z",
			"created_at": "2026-09-15T10:00:00.123Z"
		},
		{
			"id": 2,
			"certificate": "-----BEGIN CERTIFICATE-----\nMIIBszCCAVqgAwIBAgIUJ...\n-----END CERTIFICATE-----\n",
			"active": true,
			"expires_at": "2036-10-22T07:08:25.000Z",
			"created_at": "2026-10-22T07:08:25.643Z"
		}
		]`)
	})

	// WHEN listing the first page of the project's signing certificates
	certificates, resp, err := client.Attestations.ListSigningCertificates("1337", &ListSigningCertificatesOptions{
		ListOptions: ListOptions{Page: 1, PerPage: 2},
	})
	require.NoError(t, err)
	assert.NotNil(t, resp)

	// THEN both certificates are returned, including the inactive one
	want := []*SigningCertificate{
		{
			ID:          1,
			Certificate: "-----BEGIN CERTIFICATE-----\nMIIBjTCCATSgAwIBAgIUVlaC...\n-----END CERTIFICATE-----\n",
			Active:      false,
			ExpiresAt:   mustParseTime("2036-09-15T10:00:00.000Z"),
			CreatedAt:   mustParseTime("2026-09-15T10:00:00.123Z"),
		},
		{
			ID:          2,
			Certificate: "-----BEGIN CERTIFICATE-----\nMIIBszCCAVqgAwIBAgIUJ...\n-----END CERTIFICATE-----\n",
			Active:      true,
			ExpiresAt:   mustParseTime("2036-10-22T07:08:25.000Z"),
			CreatedAt:   mustParseTime("2026-10-22T07:08:25.643Z"),
		},
	}
	assert.Equal(t, want, certificates)
}
+45
Changes for testing/attestations_mock.go: 45 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -128,3 +128,48 @@ func (c *MockAttestationsServiceInterfaceListAttestationsCall) DoAndReturn(f fun
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// ListSigningCertificates mocks base method.
func (m *MockAttestationsServiceInterface) ListSigningCertificates(pid any, opt *gitlab.ListSigningCertificatesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.SigningCertificate, *gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{pid, opt}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "ListSigningCertificates", varargs...)
	ret0, _ := ret[0].([]*gitlab.SigningCertificate)
	ret1, _ := ret[1].(*gitlab.Response)
	ret2, _ := ret[2].(error)
	return ret0, ret1, ret2
}

// ListSigningCertificates indicates an expected call of ListSigningCertificates.
func (mr *MockAttestationsServiceInterfaceMockRecorder) ListSigningCertificates(pid, opt any, options ...any) *MockAttestationsServiceInterfaceListSigningCertificatesCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{pid, opt}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListSigningCertificates", reflect.TypeOf((*MockAttestationsServiceInterface)(nil).ListSigningCertificates), varargs...)
	return &MockAttestationsServiceInterfaceListSigningCertificatesCall{Call: call}
}

// MockAttestationsServiceInterfaceListSigningCertificatesCall wrap *gomock.Call
type MockAttestationsServiceInterfaceListSigningCertificatesCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockAttestationsServiceInterfaceListSigningCertificatesCall) Return(arg0 []*gitlab.SigningCertificate, arg1 *gitlab.Response, arg2 error) *MockAttestationsServiceInterfaceListSigningCertificatesCall {
	c.Call = c.Call.Return(arg0, arg1, arg2)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockAttestationsServiceInterfaceListSigningCertificatesCall) Do(f func(any, *gitlab.ListSigningCertificatesOptions, ...gitlab.RequestOptionFunc) ([]*gitlab.SigningCertificate, *gitlab.Response, error)) *MockAttestationsServiceInterfaceListSigningCertificatesCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockAttestationsServiceInterfaceListSigningCertificatesCall) DoAndReturn(f func(any, *gitlab.ListSigningCertificatesOptions, ...gitlab.RequestOptionFunc) ([]*gitlab.SigningCertificate, *gitlab.Response, error)) *MockAttestationsServiceInterfaceListSigningCertificatesCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}