github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/image/asc.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package image
    16  
    17  import (
    18  	"errors"
    19  	"net/url"
    20  	"os"
    21  
    22  	"github.com/coreos/rkt/store"
    23  	"github.com/hashicorp/errwrap"
    24  )
    25  
    26  // ascFetcher is an interface used by asc to get the desired signature
    27  // file.
    28  type ascFetcher interface {
    29  	// Get fetches the file from passed location.
    30  	Get(location string) (readSeekCloser, error)
    31  }
    32  
    33  // localAscFetcher is an implementation of ascFetcher getting
    34  // signature files from a local filesystem.
    35  type localAscFetcher struct{}
    36  
    37  func (*localAscFetcher) Get(location string) (readSeekCloser, error) {
    38  	return os.Open(location)
    39  }
    40  
    41  // remoteAscFetcher is an implementation of ascFetcher getting
    42  // signature files from remote locations.
    43  type remoteAscFetcher struct {
    44  	// F is a function that actually does the fetching
    45  	F func(*url.URL, *os.File) error
    46  	// S is a store - used for getting a temporary file
    47  	S *store.Store
    48  }
    49  
    50  func (f *remoteAscFetcher) Get(location string) (readSeekCloser, error) {
    51  	roc, err := getTmpROC(f.S, location)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	defer func() { maybeClose(roc) }()
    56  
    57  	u, err := url.Parse(location)
    58  	if err != nil {
    59  		return nil, errwrap.Wrap(errors.New("invalid signature location"), err)
    60  	}
    61  	if err := f.F(u, roc.File); err != nil {
    62  		return nil, err
    63  	}
    64  	retRoc := roc
    65  	roc = nil
    66  	return retRoc, nil
    67  }
    68  
    69  // asc is an abstraction for getting signature files.
    70  type asc struct {
    71  	// Location is a string passed to the Fetcher.
    72  	Location string
    73  	// Fetcher (if available) does the actual fetching of a
    74  	// signature key.
    75  	Fetcher ascFetcher
    76  }
    77  
    78  // Get fetches a signature file. It returns nil and no error if there
    79  // was no fetcher set.
    80  func (a *asc) Get() (readSeekCloser, error) {
    81  	if a.Fetcher != nil {
    82  		return a.Fetcher.Get(a.Location)
    83  	}
    84  	return nil, nil
    85  }