licensing

package module
v0.0.0-...-5f0f127 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: Apache-2.0 Imports: 23 Imported by: 0

README

docker/licensing

Overview

licensing is a library for interacting with Docker issued product licenses. It facilitates user's authentication to the Docker Hub, provides a mechanism for retrieving a user's existing docker-issued subscriptions/licenses, detects and verifies locally stored licenses, and can be used to provision trial licenses for Docker Enterprise Edition.

License

docker/licensing is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Usage

package main

import (
	"context"
	"fmt"
	"net/url"

	"github.com/docker/licensing"
	"github.com/docker/licensing/model"
)

const (
	hubURL      = "https://hub.docker.com"
	pubKey      = "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0Ka2lkOiBKN0xEOjY3VlI6TDVIWjpVN0JBOjJPNEc6NEFMMzpPRjJOOkpIR0I6RUZUSDo1Q1ZROk1GRU86QUVJVAoKTUlJQ0lqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FnOEFNSUlDQ2dLQ0FnRUF5ZEl5K2xVN283UGNlWSs0K3MrQwpRNU9FZ0N5RjhDeEljUUlXdUs4NHBJaVpjaVk2NzMweUNZbndMU0tUbHcrVTZVQy9RUmVXUmlvTU5ORTVEczVUCllFWGJHRzZvbG0ycWRXYkJ3Y0NnKzJVVUgvT2NCOVd1UDZnUlBIcE1GTXN4RHpXd3ZheThKVXVIZ1lVTFVwbTEKSXYrbXE3bHA1blEvUnhyVDBLWlJBUVRZTEVNRWZHd20zaE1PL2dlTFBTK2hnS1B0SUhsa2c2L1djb3hUR29LUAo3OWQvd2FIWXhHTmw3V2hTbmVpQlN4YnBiUUFLazIxbGc3OThYYjd2WnlFQVRETXJSUjlNZUU2QWRqNUhKcFkzCkNveVJBUENtYUtHUkNLNHVvWlNvSXUwaEZWbEtVUHliYncwMDBHTyt3YTJLTjhVd2dJSW0waTVJMXVXOUdrcTQKempCeTV6aGdxdVVYYkc5YldQQU9ZcnE1UWE4MUR4R2NCbEp5SFlBcCtERFBFOVRHZzR6WW1YakpueFpxSEVkdQpHcWRldlo4WE1JMHVrZmtHSUkxNHdVT2lNSUlJclhsRWNCZi80Nkk4Z1FXRHp4eWNaZS9KR1grTEF1YXlYcnlyClVGZWhWTlVkWlVsOXdYTmFKQitrYUNxejVRd2FSOTNzR3crUVNmdEQwTnZMZTdDeU9IK0U2dmc2U3QvTmVUdmcKdjhZbmhDaVhJbFo4SE9mSXdOZTd0RUYvVWN6NU9iUHlrbTN0eWxyTlVqdDBWeUFtdHRhY1ZJMmlHaWhjVVBybQprNGxWSVo3VkQvTFNXK2k3eW9TdXJ0cHNQWGNlMnBLRElvMzBsSkdoTy8zS1VtbDJTVVpDcXpKMXlFbUtweXNICjVIRFc5Y3NJRkNBM2RlQWpmWlV2TjdVQ0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo="
	username    = "docker username"
	password    = "your password"
	appFeature  = "jump"
)

func panicOnErr(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	hubURI, err := url.Parse(hubURL)
	panicOnErr(err)

	// setup client
	c, err := licensing.New(&licensing.Config{
		BaseURI:    *hubURI,
		HTTPClient: nil,
		PublicKeys: []string{pubKey},
	})
	panicOnErr(err)

	// grab token
	ctx := context.Background()
	token, err := c.LoginViaAuth(ctx, username, password)
	panicOnErr(err)

	// fetch dockerID, if not already known
	id, err := c.GetHubUserByName(ctx, username)
	panicOnErr(err)

	subs, err := c.ListSubscriptions(ctx, token, id.ID)
	panicOnErr(err)

	// find first available subscription with given feature
	var featuredSub *model.Subscription
	for _, sub := range subs {
		_, ok := sub.GetFeatureValue(appFeature)
		if ok {
			featuredSub = sub
			break
		}
	}
	if featuredSub == nil {
		fmt.Println("account has no subscriptions with the desired feature entitlements")
		return
	}

	// download license file for this subscription
	subLic, err := c.DownloadLicenseFromHub(ctx, token, featuredSub.ID)
	panicOnErr(err)

	// verify license is issued by corresponding keypair and is not expired
	licFile, err := c.VerifyLicense(ctx, *subLic)
	panicOnErr(err)

	fmt.Println("license summary: ", c.SummarizeLicense(licFile))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrWorkerNode returned on a swarm worker node - lookup licenses on swarm managers
	ErrWorkerNode = fmt.Errorf("this node is not a swarm manager - check license status on a manager node")
	// ErrUnlicensed returned when no license found
	ErrUnlicensed = fmt.Errorf("no license found")
)

Functions

func StoreLicense

func StoreLicense(ctx context.Context, clnt WrappedDockerClient, license *model.IssuedLicense, rootDir string) error

StoreLicense will store the license on the host filesystem and swarm (if swarm is active)

Types

type Client

type Client interface {
	LoginViaAuth(ctx context.Context, username, password string) (authToken string, err error)
	GetHubUserOrgs(ctx context.Context, authToken string) (orgs []model.Org, err error)
	GetHubUserByName(ctx context.Context, username string) (user *model.User, err error)
	VerifyLicense(ctx context.Context, license model.IssuedLicense) (res *model.CheckResponse, err error)
	GenerateNewTrialSubscription(ctx context.Context, authToken, dockerID string) (subscriptionID string, err error)
	ListSubscriptions(ctx context.Context, authToken, dockerID string) (response []*model.Subscription, err error)
	ListSubscriptionsDetails(ctx context.Context, authToken, dockerID string) (response []*model.SubscriptionDetail, err error)
	DownloadLicenseFromHub(ctx context.Context, authToken, subscriptionID string) (license *model.IssuedLicense, err error)
	ParseLicense(license []byte) (parsedLicense *model.IssuedLicense, err error)
	StoreLicense(ctx context.Context, dclnt WrappedDockerClient, licenses *model.IssuedLicense, localRootDir string) error
	LoadLocalLicense(ctx context.Context, dclnt WrappedDockerClient) (*model.Subscription, error)
	SummarizeLicense(res *model.CheckResponse) *model.Subscription
}

Client represents the licensing package interface, including methods for authentication and interaction with Docker licensing, accounts, and billing services

func New

func New(config *Config) (Client, error)

New creates a new licensing Client

type Config

type Config struct {
	BaseURI    url.URL
	HTTPClient *http.Client
	// used by licensing client to validate an issued license
	PublicKeys []string
}

Config holds licensing client configuration

type RequestParams

type RequestParams struct {
	DockerID         string
	PartnerAccountID string
	Origin           string
}

RequestParams holds request parameters

type WrappedDockerClient

type WrappedDockerClient interface {
	Info(ctx context.Context) (types.Info, error)
	NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
	ConfigCreate(ctx context.Context, config swarm.ConfigSpec) (types.ConfigCreateResponse, error)
	ConfigList(ctx context.Context, options types.ConfigListOptions) ([]swarm.Config, error)
	ConfigInspectWithRaw(ctx context.Context, id string) (swarm.Config, []byte, error)
}

WrappedDockerClient provides methods useful for installing licenses to the wrapped docker engine or cluster

Directories

Path Synopsis
lib
errors
Package errors provides error and error wrapping facilities that allow for the easy reporting of call stacks and structured error annotations.
Package errors provides error and error wrapping facilities that allow for the easy reporting of call stacks and structured error annotations.

Jump to

Keyboard shortcuts

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