...

Source file src/github.com/sigstore/rekor/pkg/storage/storage.go

Documentation: github.com/sigstore/rekor/pkg/storage

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package storage
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/sigstore/rekor/pkg/log"
    24  
    25  	"github.com/spf13/viper"
    26  	"gocloud.dev/blob"
    27  
    28  	// Blank imports to register storage
    29  	_ "gocloud.dev/blob/fileblob"
    30  	_ "gocloud.dev/blob/gcsblob"
    31  	_ "gocloud.dev/blob/memblob"
    32  	_ "gocloud.dev/blob/s3blob"
    33  )
    34  
    35  type AttestationStorage interface {
    36  	StoreAttestation(ctx context.Context, key string, attestation []byte) error
    37  	FetchAttestation(ctx context.Context, key string) ([]byte, error)
    38  }
    39  
    40  func NewAttestationStorage() (AttestationStorage, error) {
    41  	if url := viper.GetString("attestation_storage_bucket"); url != "" {
    42  		log.Logger.Infof("Configuring attestation storage at %s", url)
    43  		bucket, err := blob.OpenBucket(context.Background(), url)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		return &Blob{
    48  			bucket: bucket,
    49  		}, nil
    50  	}
    51  	return nil, errors.New("no storage configured")
    52  }
    53  
    54  type Blob struct {
    55  	bucket *blob.Bucket
    56  }
    57  
    58  func (b *Blob) StoreAttestation(ctx context.Context, key string, attestation []byte) error {
    59  	log.ContextLogger(ctx).Infof("storing attestation at %s", key)
    60  	w, err := b.bucket.NewWriter(ctx, key, nil)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	if _, err := w.Write(attestation); err != nil {
    65  		return err
    66  	}
    67  	return w.Close()
    68  }
    69  
    70  func (b *Blob) FetchAttestation(ctx context.Context, key string) ([]byte, error) {
    71  	log.ContextLogger(ctx).Infof("fetching attestation %s", key)
    72  	exists, err := b.bucket.Exists(ctx, key)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	if !exists {
    77  		return nil, fmt.Errorf("attestation %v does not exist", key)
    78  	}
    79  
    80  	data, err := b.bucket.ReadAll(ctx, key)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return data, nil
    85  }
    86  

View as plain text