store

package
v2.42.5-go-mod+incompa... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2020 License: GPL-3.0 Imports: 41 Imported by: 0

Documentation

Overview

Package store has support to use the Ubuntu Store for querying and downloading of snaps, and the related services.

Index

Constants

View Source
const (

	// UbuntuCoreWireProtocol is the protocol level we support when
	// communicating with the store. History:
	//  - "1": client supports squashfs snaps
	UbuntuCoreWireProtocol = "1"
)

Variables

View Source
var (

	// macaroonACLAPI points to Developer API endpoint to get an ACL macaroon
	MacaroonACLAPI = developerAPIBase + "dev/api/acl/"

	// UbuntuoneLocation is the Ubuntuone location as defined in the store macaroon
	UbuntuoneLocation = authLocation()
	// UbuntuoneDischargeAPI points to SSO endpoint to discharge a macaroon
	UbuntuoneDischargeAPI = ubuntuoneAPIBase + "/tokens/discharge"
	// UbuntuoneRefreshDischargeAPI points to SSO endpoint to refresh a discharge macaroon
	UbuntuoneRefreshDischargeAPI = ubuntuoneAPIBase + "/tokens/refresh"
)
View Source
var (
	// ErrBadQuery is returned from Find when the query has special characters in strange places.
	ErrBadQuery = errors.New("bad query")

	// ErrSnapNotFound is returned when a snap can not be found
	ErrSnapNotFound = errors.New("snap not found")

	// ErrUnauthenticated is returned when authentication is needed to complete the query
	ErrUnauthenticated = errors.New("you need to log in first")

	// ErrAuthenticationNeeds2fa is returned if the authentication needs 2factor
	ErrAuthenticationNeeds2fa = errors.New("two factor authentication required")

	// Err2faFailed is returned when 2fa failed (e.g., a bad token was given)
	Err2faFailed = errors.New("two factor authentication failed")

	// ErrInvalidCredentials is returned on login error
	// It can also be returned when refreshing the discharge
	// macaroon if the user has changed their password.
	ErrInvalidCredentials = errors.New("invalid credentials")

	// ErrTOSNotAccepted is returned when the user has not accepted the store's terms of service.
	ErrTOSNotAccepted = errors.New("terms of service not accepted")

	// ErrNoPaymentMethods is returned when the user has no valid payment methods associated with their account.
	ErrNoPaymentMethods = errors.New("no payment methods")

	// ErrPaymentDeclined is returned when the user's payment method was declined by the upstream payment provider.
	ErrPaymentDeclined = errors.New("payment declined")

	// ErrLocalSnap is returned when an operation that only applies to snaps that come from a store was attempted on a local snap.
	ErrLocalSnap = errors.New("cannot perform operation on local snap")

	// ErrNoUpdateAvailable is returned when an update is attempetd for a snap that has no update available.
	ErrNoUpdateAvailable = errors.New("snap has no updates available")
)
View Source
var (
	// ErrNoSerial indicates that a device serial is not set yet.
	ErrNoSerial = errors.New("no device serial yet")
)
View Source
var ErrTooManyRequests = errors.New("too many requests")

Functions

func ClientUserAgent

func ClientUserAgent(ctx context.Context) string

ClientUserAgent returns the user agent of the client that talks to snapd

func WithClientUserAgent

func WithClientUserAgent(parent context.Context, req *http.Request) context.Context

ClientUserAgentContext carries the client user agent that talks to snapd

Types

type CacheManager

type CacheManager struct {
	// contains filtered or unexported fields
}

cacheManager implements a downloadCache via content based hard linking

func NewCacheManager

func NewCacheManager(cacheDir string, maxItems int) *CacheManager

NewCacheManager returns a new CacheManager with the given cacheDir and the given maximum amount of items. The idea behind it is the following algorithm:

  1. When starting a download, check if it exists in $cacheDir
  2. If found, update its mtime, hardlink into target location, and return success
  3. If not found, download the snap
  4. On success, hardlink into $cacheDir/<digest>
  5. If cache dir has more than maxItems entries, remove oldest mtimes until it has maxItems

The caching part is done here, the downloading happens in the store.go code.

func (*CacheManager) Get

func (cm *CacheManager) Get(cacheKey, targetPath string) error

Get gets the given cacheKey content and puts it into targetPath

func (*CacheManager) GetPath

func (cm *CacheManager) GetPath(cacheKey string) string

GetPath returns the full path of the given content in the cache or empty string

func (*CacheManager) Put

func (cm *CacheManager) Put(cacheKey, sourcePath string) error

Put adds a new file to the cache with the given cacheKey

type Config

type Config struct {
	// Store API base URLs. The assertions url is only separate because it can
	// be overridden by its own env var.
	StoreBaseURL      *url.URL
	AssertionsBaseURL *url.URL

	// StoreID is the store id used if we can't get one through the DeviceAndAuthContext.
	StoreID string

	Architecture string
	Series       string

	DetailFields []string
	InfoFields   []string
	DeltaFormat  string

	// CacheDownloads is the number of downloads that should be cached
	CacheDownloads int

	// Proxy returns the HTTP proxy to use when talking to the store
	Proxy func(*http.Request) (*url.URL, error)
}

Config represents the configuration to access the snap store

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a copy of the default configuration ready to be adapted.

type CurrentSnap

type CurrentSnap struct {
	InstanceName     string
	SnapID           string
	Revision         snap.Revision
	TrackingChannel  string
	RefreshedDate    time.Time
	IgnoreValidation bool
	Block            []snap.Revision
	Epoch            snap.Epoch
	CohortKey        string
}

type DeviceAndAuthContext

type DeviceAndAuthContext interface {
	Device() (*auth.DeviceState, error)

	UpdateDeviceAuth(device *auth.DeviceState, sessionMacaroon string) (actual *auth.DeviceState, err error)

	UpdateUserAuth(user *auth.UserState, discharges []string) (actual *auth.UserState, err error)

	StoreID(fallback string) (string, error)

	DeviceSessionRequestParams(nonce string) (*DeviceSessionRequestParams, error)
	ProxyStoreParams(defaultURL *url.URL) (proxyStoreID string, proxySroreURL *url.URL, err error)

	CloudInfo() (*auth.CloudInfo, error)
}

A DeviceAndAuthContext mediates access to device and auth information for the store.

type DeviceSessionRequestParams

type DeviceSessionRequestParams struct {
	Request *asserts.DeviceSessionRequest
	Serial  *asserts.Serial
	Model   *asserts.Model
}

DeviceSessionRequestParams gathers the assertions and information to be sent to request a device session.

func (*DeviceSessionRequestParams) EncodedModel

func (p *DeviceSessionRequestParams) EncodedModel() string

func (*DeviceSessionRequestParams) EncodedRequest

func (p *DeviceSessionRequestParams) EncodedRequest() string

func (*DeviceSessionRequestParams) EncodedSerial

func (p *DeviceSessionRequestParams) EncodedSerial() string

type DownloadError

type DownloadError struct {
	Code int
	URL  *url.URL
}

DownloadError represents a download error

func (*DownloadError) Error

func (e *DownloadError) Error() string

type DownloadOptions

type DownloadOptions struct {
	RateLimit           int64
	IsAutoRefresh       bool
	LeavePartialOnError bool
}

type HashError

type HashError struct {
	// contains filtered or unexported fields
}

func (HashError) Error

func (e HashError) Error() string

type InvalidAuthDataError

type InvalidAuthDataError map[string]stringList

InvalidAuthDataError signals that the authentication data didn't pass validation.

func (InvalidAuthDataError) Error

func (e InvalidAuthDataError) Error() string

type PasswordPolicyError

type PasswordPolicyError map[string]stringList

PasswordPolicyError is returned in a few corner cases, most notably when the password has been force-reset.

func (PasswordPolicyError) Error

func (e PasswordPolicyError) Error() string

type RefreshOptions

type RefreshOptions struct {
	// RefreshManaged indicates to the store that the refresh is
	// managed via snapd-control.
	RefreshManaged bool
	IsAutoRefresh  bool

	PrivacyKey string
}

type RevisionNotAvailableError

type RevisionNotAvailableError struct {
	Action   string
	Channel  string
	Releases []channel.Channel
}

RevisionNotAvailableError is returned when an install is attempted for a snap but the/a revision is not available (given install constraints).

func (*RevisionNotAvailableError) Error

func (e *RevisionNotAvailableError) Error() string
type Search struct {
	// Query is a term to search by or a prefix (if Prefix is true)
	Query  string
	Prefix bool

	CommonID string

	Section string
	Private bool
	Scope   string
}

A Search is what you do in order to Find something

type SnapAction

type SnapAction struct {
	Action       string
	InstanceName string
	SnapID       string
	Channel      string
	Revision     snap.Revision
	CohortKey    string
	Flags        SnapActionFlags
	Epoch        snap.Epoch
}

type SnapActionError

type SnapActionError struct {
	// NoResults is set if the there were no results in the response
	NoResults bool
	// Refresh errors by snap name.
	Refresh map[string]error
	// Install errors by snap name.
	Install map[string]error
	// Download errors by snap name.
	Download map[string]error
	// Other errors.
	Other []error
}

SnapActionError conveys errors that were reported on otherwise overall successful snap action (install/refresh) request.

func (SnapActionError) Error

func (e SnapActionError) Error() string

type SnapActionFlags

type SnapActionFlags int
const (
	SnapActionIgnoreValidation SnapActionFlags = 1 << iota
	SnapActionEnforceValidation
)

type SnapAdder

type SnapAdder interface {
	AddSnap(snapName, version, summary string, commands []string) error
}

type SnapSpec

type SnapSpec struct {
	Name string
}

A SnapSpec describes a single snap wanted from SnapInfo

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store represents the ubuntu snap store

func New

func New(cfg *Config, dauthCtx DeviceAndAuthContext) *Store

New creates a new Store with the given access configuration and for given the store id.

func (*Store) Assertion

func (s *Store) Assertion(assertType *asserts.AssertionType, primaryKey []string, user *auth.UserState) (asserts.Assertion, error)

Assertion retrivies the assertion for the given type and primary key.

func (*Store) Buy

func (s *Store) Buy(options *client.BuyOptions, user *auth.UserState) (*client.BuyResult, error)

Buy sends a buy request for the specified snap. Returns the state of the order: Complete, Cancelled.

func (*Store) CacheDownloads

func (s *Store) CacheDownloads() int

func (*Store) ConnectivityCheck

func (s *Store) ConnectivityCheck() (status map[string]bool, err error)

func (*Store) CreateCohorts

func (s *Store) CreateCohorts(ctx context.Context, snaps []string) (map[string]string, error)

func (*Store) Download

func (s *Store) Download(ctx context.Context, name string, targetPath string, downloadInfo *snap.DownloadInfo, pbar progress.Meter, user *auth.UserState, dlOpts *DownloadOptions) error

Download downloads the snap addressed by download info and returns its filename. The file is saved in temporary storage, and should be removed after use to prevent the disk from running out of space.

func (*Store) DownloadStream

func (s *Store) DownloadStream(ctx context.Context, name string, downloadInfo *snap.DownloadInfo, user *auth.UserState) (io.ReadCloser, error)

DownloadStream will copy the snap from the request to the io.Reader

func (*Store) EnsureDeviceSession

func (s *Store) EnsureDeviceSession() (*auth.DeviceState, error)

EnsureDeviceSession makes sure the store has a device session available. Expects the store to have an AuthContext.

func (*Store) Find

func (s *Store) Find(ctx context.Context, search *Search, user *auth.UserState) ([]*snap.Info, error)

Find finds (installable) snaps from the store, matching the given Search.

func (*Store) LoginUser

func (s *Store) LoginUser(username, password, otp string) (string, string, error)

LoginUser logs user in the store and returns the authentication macaroons.

func (*Store) ReadyToBuy

func (s *Store) ReadyToBuy(user *auth.UserState) error

ReadyToBuy returns nil if the user's account has accepted T&Cs and has a payment method registered, and an error otherwise

func (*Store) Sections

func (s *Store) Sections(ctx context.Context, user *auth.UserState) ([]string, error)

Sections retrieves the list of available store sections.

func (*Store) SetCacheDownloads

func (s *Store) SetCacheDownloads(fileCount int)

func (*Store) SnapAction

func (s *Store) SnapAction(ctx context.Context, currentSnaps []*CurrentSnap, actions []*SnapAction, user *auth.UserState, opts *RefreshOptions) ([]*snap.Info, error)

SnapAction queries the store for snap information for the given install/refresh actions, given the context information about current installed snaps in currentSnaps. If the request was overall successul (200) but there were reported errors it will return both the snap infos and an SnapActionError.

func (*Store) SnapInfo

func (s *Store) SnapInfo(ctx context.Context, snapSpec SnapSpec, user *auth.UserState) (*snap.Info, error)

SnapInfo returns the snap.Info for the store-hosted snap matching the given spec, or an error.

func (*Store) SuggestedCurrency

func (s *Store) SuggestedCurrency() string

SuggestedCurrency retrieves the cached value for the store's suggested currency

func (*Store) UserInfo

func (s *Store) UserInfo(email string) (userinfo *User, err error)

func (*Store) WriteCatalogs

func (s *Store) WriteCatalogs(ctx context.Context, names io.Writer, adder SnapAdder) error

WriteCatalogs queries the "commands" endpoint and writes the command names into the given io.Writer.

type User

type User struct {
	Username         string
	SSHKeys          []string
	OpenIDIdentifier string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL