<commit_before>package gopinba

import (
	"fmt"
	"net"
	"sync"
	"time"

	"github.com/gogo/protobuf/proto"
	"github.com/mkevac/gopinba/Pinba"
)

type Client struct {
	initialized bool
	address     string
	conn        net.Conn
}

type Timer struct {
	Tags map[string]string

	// private stuff for simpler api
	stopped  bool
	started  time.Time
	duration time.Duration
}

type Request struct {
	Hostname     string
	ServerName   string
	ScriptName   string
	RequestCount uint32
	RequestTime  time.Duration
	DocumentSize uint32
	MemoryPeak   uint32
	Utime        float32
	Stime        float32
	timers       []Timer
	Status       uint32
	lk           sync.Mutex
}

func NewClient(address string) (*Client, error) {
	pc := &Client{address: address}
	conn, err := net.Dial("udp", address)
	if err != nil {
		return nil, err
	}

	pc.conn = conn
	pc.initialized = true

	return pc, nil
}

func iN(haystack []string, needle string) (int, bool) {
	for i, s := range haystack {
		if s == needle {
			return i, true
		}
	}
	return -1, false
}

func useAndUpdateDictionary(dictionary []string, tags map[string]string) (map[uint32]uint32, []string) {
	newTags := make(map[uint32]uint32)
	newDict := dictionary

	for k, v := range tags {

		var kpos int
		var vpos int
		var pos int
		var exist bool

		pos, exist = iN(newDict, k)
		if exist {
			kpos = pos
		} else {
			newDict = append(newDict, k)
			kpos = len(newDict) - 1
		}

		pos, exist = iN(newDict, v)
		if exist {
			vpos = pos
		} else {
			newDict = append(newDict, v)
			vpos = len(newDict) - 1
		}

		newTags[uint32(kpos)] = uint32(vpos)
	}

	return newTags, newDict
}

func (pc *Client) SendRequest(request *Request) error {

	if !pc.initialized {
		return fmt.Errorf("Client not initialized")
	}

	pbreq := Pinba.Request{
		Hostname:      request.Hostname,
		ServerName:    request.ServerName,
		ScriptName:    request.ScriptName,
		RequestCount:  request.RequestCount,
		RequestTime:   float32(request.RequestTime.Seconds()),
		DocumentSize:  request.DocumentSize,
		MemoryPeak:    request.MemoryPeak,
		RuUtime:       request.Utime,
		RuStime:       request.Stime,
		Status:        request.Status,
		TimerHitCount: make([]uint32, 0),
		TimerValue:    make([]float32, 0),
		Dictionary:    make([]string, 0),
	}

	for _, timer := range request.timers {
		pbreq.TimerHitCount = append(pbreq.TimerHitCount, 1)
		pbreq.TimerValue = append(pbreq.TimerValue, float32(timer.duration.Seconds()))
		tagsMap, newDict := useAndUpdateDictionary(pbreq.Dictionary, timer.Tags)
		pbreq.Dictionary = newDict
		pbreq.TimerTagCount = append(pbreq.TimerTagCount, uint32(len(tagsMap)))

		for k, v := range tagsMap {
			pbreq.TimerTagName = append(pbreq.TimerTagName, k)
			pbreq.TimerTagValue = append(pbreq.TimerTagValue, v)
		}
	}

	buf, err := proto.Marshal(&pbreq)
	if err != nil {
		return err
	}

	_, err = pc.conn.Write(buf)
	if err != nil {
		return err
	}

	return nil
}

func (req *Request) AddTimer(timer *Timer) {
	req.lk.Lock()
	defer req.lk.Unlock()

	req.timers = append(req.timers, *timer)
}

// this is exactly the same as AddTimer
//  exists only to have api naming similar to pinba php extension
func (req *Request) TimerAdd(timer *Timer) {
	timer.Stop()
	req.AddTimer(timer)
}

func TimerStart(tags map[string]string) *Timer {
	return &Timer{
		duration: 0,
		Tags:     tags,
		stopped:  false,
		started:  time.Now(),
	}
}

func NewTimer(tags map[string]string, duration time.Duration) *Timer {
	return &Timer{
		duration: duration,
		Tags:     tags,
		stopped:  true,
		started:  time.Now().Add(-duration),
	}
}

func (t *Timer) Stop() {
	if !t.stopped {
		t.stopped = true
		t.duration = time.Now().Sub(t.started)
	}
}

func (t *Timer) GetDuration() time.Duration {
	return t.duration
}
<commit_msg>Request.Tags impl<commit_after>package gopinba

import (
	"fmt"
	"net"
	"sync"
	"time"

	"github.com/gogo/protobuf/proto"
	"github.com/mkevac/gopinba/Pinba"
)

type Client struct {
	initialized bool
	address     string
	conn        net.Conn
}

type Timer struct {
	Tags map[string]string

	// private stuff for simpler api
	stopped  bool
	started  time.Time
	duration time.Duration
}

type Request struct {
	Hostname     string
	ServerName   string
	ScriptName   string
	RequestCount uint32
	RequestTime  time.Duration
	DocumentSize uint32
	MemoryPeak   uint32
	Utime        float32
	Stime        float32
	timers       []Timer
	Status       uint32
	Tags         map[string]string
	lk           sync.Mutex
}

func NewClient(address string) (*Client, error) {
	pc := &Client{address: address}
	conn, err := net.Dial("udp", address)
	if err != nil {
		return nil, err
	}

	pc.conn = conn
	pc.initialized = true

	return pc, nil
}

func iN(haystack []string, needle string) (int, bool) {
	for i, s := range haystack {
		if s == needle {
			return i, true
		}
	}
	return -1, false
}

func useAndUpdateDictionary(dictionary []string, tags map[string]string) (map[uint32]uint32, []string) {
	newTags := make(map[uint32]uint32)
	newDict := dictionary

	for k, v := range tags {

		var kpos int
		var vpos int
		var pos int
		var exist bool

		pos, exist = iN(newDict, k)
		if exist {
			kpos = pos
		} else {
			newDict = append(newDict, k)
			kpos = len(newDict) - 1
		}

		pos, exist = iN(newDict, v)
		if exist {
			vpos = pos
		} else {
			newDict = append(newDict, v)
			vpos = len(newDict) - 1
		}

		newTags[uint32(kpos)] = uint32(vpos)
	}

	return newTags, newDict
}

func (pc *Client) SendRequest(request *Request) error {

	if !pc.initialized {
		return fmt.Errorf("Client not initialized")
	}

	pbreq := Pinba.Request{
		Hostname:      request.Hostname,
		ServerName:    request.ServerName,
		ScriptName:    request.ScriptName,
		RequestCount:  request.RequestCount,
		RequestTime:   float32(request.RequestTime.Seconds()),
		DocumentSize:  request.DocumentSize,
		MemoryPeak:    request.MemoryPeak,
		RuUtime:       request.Utime,
		RuStime:       request.Stime,
		Status:        request.Status,
		TimerHitCount: make([]uint32, 0),
		TimerValue:    make([]float32, 0),
		TagName:       make([]uint32, 0),
		TagValue:      make([]uint32, 0),
		Dictionary:    make([]string, 0),
	}

	for _, tag := range request.Tags {
		tagsMap, newDict := useAndUpdateDictionary(pbreq.Dictionary, timer.Tags)
		pbreq.Dictionary = newDict

		for k, v := range tagsMap {
			pbreq.TagName = append(pbreq.TagName, k)
			pbreq.TagValue = append(pbreq.TagValue, v)
		}
	}

	for _, timer := range request.timers {
		pbreq.TimerHitCount = append(pbreq.TimerHitCount, 1)
		pbreq.TimerValue = append(pbreq.TimerValue, float32(timer.duration.Seconds()))
		tagsMap, newDict := useAndUpdateDictionary(pbreq.Dictionary, timer.Tags)
		pbreq.Dictionary = newDict
		pbreq.TimerTagCount = append(pbreq.TimerTagCount, uint32(len(tagsMap)))

		for k, v := range tagsMap {
			pbreq.TimerTagName = append(pbreq.TimerTagName, k)
			pbreq.TimerTagValue = append(pbreq.TimerTagValue, v)
		}
	}

	buf, err := proto.Marshal(&pbreq)
	if err != nil {
		return err
	}

	_, err = pc.conn.Write(buf)
	if err != nil {
		return err
	}

	return nil
}

func (req *Request) AddTimer(timer *Timer) {
	req.lk.Lock()
	defer req.lk.Unlock()

	req.timers = append(req.timers, *timer)
}

// this is exactly the same as AddTimer
//  exists only to have api naming similar to pinba php extension
func (req *Request) TimerAdd(timer *Timer) {
	timer.Stop()
	req.AddTimer(timer)
}

func TimerStart(tags map[string]string) *Timer {
	return &Timer{
		duration: 0,
		Tags:     tags,
		stopped:  false,
		started:  time.Now(),
	}
}

func NewTimer(tags map[string]string, duration time.Duration) *Timer {
	return &Timer{
		duration: duration,
		Tags:     tags,
		stopped:  true,
		started:  time.Now().Add(-duration),
	}
}

func (t *Timer) Stop() {
	if !t.stopped {
		t.stopped = true
		t.duration = time.Now().Sub(t.started)
	}
}

func (t *Timer) GetDuration() time.Duration {
	return t.duration
}
<|endoftext|>


<commit_before>package chat

import (
	"fmt"
	"strings"

	"github.com/keybase/client/go/chat/globals"
	"github.com/keybase/client/go/chat/types"
	"github.com/keybase/client/go/chat/utils"
	"github.com/keybase/client/go/protocol/keybase1"
	"github.com/keybase/client/go/teams"
	context "golang.org/x/net/context"
)

type TeamsNameInfoSource struct {
	globals.Contextified
	utils.DebugLabeler
}

func NewTeamsNameInfoSource(g *globals.Context) *TeamsNameInfoSource {
	return &TeamsNameInfoSource{
		Contextified: globals.NewContextified(g),
		DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "TeamsNameInfoSource", false),
	}
}

func (t *TeamsNameInfoSource) Lookup(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	defer t.Trace(ctx, func() error { return err }, fmt.Sprintf("Lookup(%s)", name))()

	team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{
		Name:        name, // Loading by name is a last resort and will always cause an extra roundtrip.
		ForceRepoll: true,
	})
	if err != nil {
		return res, err
	}
	return teamToNameInfo(ctx, team, vis)
}

func teamToNameInfo(ctx context.Context, team *teams.Team, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	res.ID, err = teamIDToTLFID(team.ID)
	if err != nil {
		return res, err
	}
	res.CanonicalName = team.Name().String()

	if vis == keybase1.TLFVisibility_PRIVATE {
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}
	return res, nil
}

type ImplicitTeamsNameInfoSource struct {
	globals.Contextified
	utils.DebugLabeler
}

func NewImplicitTeamsNameInfoSource(g *globals.Context) *ImplicitTeamsNameInfoSource {
	return &ImplicitTeamsNameInfoSource{
		Contextified: globals.NewContextified(g),
		DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "ImplicitTeamsNameInfoSource", false),
	}
}

func (t *ImplicitTeamsNameInfoSource) Lookup(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	// check if name is prefixed
	if strings.HasPrefix(name, keybase1.ImplicitTeamPrefix) {
		return t.lookupInternalName(ctx, name, vis)
	}

	teamID, teamName, err := teams.LookupImplicitTeam(ctx, t.G().ExternalG(), name, vis == keybase1.TLFVisibility_PUBLIC)
	if err != nil {
		return res, err
	}

	if !teamID.IsRootTeam() {
		panic(fmt.Sprintf("implicit team found via LookupImplicitTeam not root team: %s", teamID))
	}

	res.ID, err = teamIDToTLFID(teamID)
	if err != nil {
		return res, err
	}

	res.CanonicalName = teamName.String()

	if vis == keybase1.TLFVisibility_PRIVATE {
		team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{ID: teamID})
		if err != nil {
			return res, err
		}
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}

	return res, nil
}

func (t *ImplicitTeamsNameInfoSource) lookupInternalName(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{Name: name})
	if err != nil {
		return res, err
	}
	res.ID, err = teamIDToTLFID(team.ID)
	if err != nil {
		return res, err
	}
	display, err := team.ImplicitTeamDisplayName(ctx)
	if err != nil {
		return res, err
	}
	res.CanonicalName = display.String()
	if vis == keybase1.TLFVisibility_PRIVATE {
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}
	return res, nil
}
<commit_msg>Implicit name source runs identify<commit_after>package chat

import (
	"errors"
	"fmt"
	"strings"

	"github.com/keybase/client/go/chat/globals"
	"github.com/keybase/client/go/chat/types"
	"github.com/keybase/client/go/chat/utils"
	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol/keybase1"
	"github.com/keybase/client/go/teams"
	context "golang.org/x/net/context"
)

type TeamsNameInfoSource struct {
	globals.Contextified
	utils.DebugLabeler
}

func NewTeamsNameInfoSource(g *globals.Context) *TeamsNameInfoSource {
	return &TeamsNameInfoSource{
		Contextified: globals.NewContextified(g),
		DebugLabeler: utils.NewDebugLabeler(g.GetLog(), "TeamsNameInfoSource", false),
	}
}

func (t *TeamsNameInfoSource) Lookup(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	defer t.Trace(ctx, func() error { return err }, fmt.Sprintf("Lookup(%s)", name))()

	team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{
		Name:        name, // Loading by name is a last resort and will always cause an extra roundtrip.
		ForceRepoll: true,
	})
	if err != nil {
		return res, err
	}
	return teamToNameInfo(ctx, team, vis)
}

func teamToNameInfo(ctx context.Context, team *teams.Team, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	res.ID, err = teamIDToTLFID(team.ID)
	if err != nil {
		return res, err
	}
	res.CanonicalName = team.Name().String()

	if vis == keybase1.TLFVisibility_PRIVATE {
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}
	return res, nil
}

type ImplicitTeamsNameInfoSource struct {
	globals.Contextified
	utils.DebugLabeler
	*NameIdentifier
}

func NewImplicitTeamsNameInfoSource(g *globals.Context) *ImplicitTeamsNameInfoSource {
	return &ImplicitTeamsNameInfoSource{
		Contextified:   globals.NewContextified(g),
		DebugLabeler:   utils.NewDebugLabeler(g.GetLog(), "ImplicitTeamsNameInfoSource", false),
		NameIdentifier: NewNameIdentifier(g),
	}
}

func (t *ImplicitTeamsNameInfoSource) Lookup(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	// check if name is prefixed
	if strings.HasPrefix(name, keybase1.ImplicitTeamPrefix) {
		return t.lookupInternalName(ctx, name, vis)
	}

	teamID, teamName, err := teams.LookupImplicitTeam(ctx, t.G().ExternalG(), name, vis == keybase1.TLFVisibility_PUBLIC)
	if err != nil {
		return res, err
	}

	if !teamID.IsRootTeam() {
		panic(fmt.Sprintf("implicit team found via LookupImplicitTeam not root team: %s", teamID))
	}

	res.ID, err = teamIDToTLFID(teamID)
	if err != nil {
		return res, err
	}

	res.CanonicalName = teamName.String()

	if vis == keybase1.TLFVisibility_PRIVATE {
		team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{ID: teamID})
		if err != nil {
			return res, err
		}
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}

	// identify the members in the conversation
	identBehavior, breaks, ok := IdentifyMode(ctx)
	if !ok {
		return res, errors.New("invalid context with no chat metadata")
	}
	query := keybase1.TLFQuery{
		TlfName:          res.CanonicalName,
		IdentifyBehavior: identBehavior,
	}
	ib, err := t.Identify(ctx, query, true)
	if err != nil {
		return res, err
	}
	// use id breaks calculated by Identify
	res.IdentifyFailures = ib

	if in := CtxIdentifyNotifier(ctx); in != nil {
		update := keybase1.CanonicalTLFNameAndIDWithBreaks{
			TlfID:         keybase1.TLFID(res.ID),
			CanonicalName: keybase1.CanonicalTlfName(res.CanonicalName),
			Breaks: keybase1.TLFBreak{
				Breaks: res.IdentifyFailures,
			},
		}
		in.Send(update)
	}
	*breaks = appendBreaks(*breaks, res.IdentifyFailures)

	// GUI Strict mode errors are swallowed earlier, return an error now (key is that it is
	// after send to IdentifyNotifier)
	if identBehavior == keybase1.TLFIdentifyBehavior_CHAT_GUI_STRICT && len(res.IdentifyFailures) > 0 {
		return res, libkb.NewIdentifySummaryError(res.IdentifyFailures[0])
	}

	return res, nil
}

func (t *ImplicitTeamsNameInfoSource) lookupInternalName(ctx context.Context, name string, vis keybase1.TLFVisibility) (res types.NameInfo, err error) {
	team, err := teams.Load(ctx, t.G().ExternalG(), keybase1.LoadTeamArg{Name: name})
	if err != nil {
		return res, err
	}
	res.ID, err = teamIDToTLFID(team.ID)
	if err != nil {
		return res, err
	}
	display, err := team.ImplicitTeamDisplayName(ctx)
	if err != nil {
		return res, err
	}
	res.CanonicalName = display.String()
	if vis == keybase1.TLFVisibility_PRIVATE {
		chatKeys, err := team.AllApplicationKeys(ctx, keybase1.TeamApplication_CHAT)
		if err != nil {
			return res, err
		}
		for _, key := range chatKeys {
			res.CryptKeys = append(res.CryptKeys, key)
		}
	}
	return res, nil
}
<|endoftext|>


<commit_before>package orcraft

import (
	"encoding/json"
	"fmt"
	"net"
	"os"
	"strings"
	"time"

	"github.com/openark/golib/log"

	"github.com/hashicorp/raft"
)

const (
	retainSnapshotCount = 288
	snapshotInterval    = 10 * time.Second
	raftTimeout         = 10 * time.Second
)

type Store struct {
	raftDir  string
	raftBind string

	raft      *raft.Raft // The consensus mechanism
	peerStore raft.PeerStore

	applier                CommandApplier
	snapshotCreatorApplier SnapshotCreatorApplier
}

type storeCommand struct {
	Op    string `json:"op,omitempty"`
	Value []byte `json:"value,omitempty"`
}

// NewStore inits and returns a new store
func NewStore(raftDir string, raftBind string, applier CommandApplier, snapshotCreatorApplier SnapshotCreatorApplier) *Store {
	return &Store{
		raftDir:                raftDir,
		raftBind:               raftBind,
		applier:                applier,
		snapshotCreatorApplier: snapshotCreatorApplier,
	}
}

// Open opens the store. If enableSingle is set, and there are no existing peers,
// then this node becomes the first node, and therefore leader, of the cluster.
func (store *Store) Open(peerNodes []string) error {
	// Setup Raft configuration.
	config := raft.DefaultConfig()
	config.SnapshotThreshold = 1
	config.SnapshotInterval = snapshotInterval

	// Setup Raft communication.
	addr, err := net.ResolveTCPAddr("tcp", store.raftBind)
	if err != nil {
		return err
	}
	log.Debugf("raft: addr=%+v", addr)
	transport, err := raft.NewTCPTransport(store.raftBind, addr, 3, 10*time.Second, os.Stderr)
	if err != nil {
		return err
	}
	log.Debugf("raft: transport=%+v", transport)

	peers := make([]string, 0, 10)
	for _, peerNode := range peerNodes {
		peerNode = strings.TrimSpace(peerNode)
		peers = raft.AddUniquePeer(peers, peerNode)
	}
	log.Debugf("raft: peers=%+v", peers)

	// Create peer storage.
	peerStore := &raft.StaticPeers{}
	if err := peerStore.SetPeers(peers); err != nil {
		return err
	}

	// Allow the node to enter single-mode, potentially electing itself, if
	// explicitly enabled and there is only 1 node in the cluster already.
	if len(peerNodes) == 0 && len(peers) <= 1 {
		log.Infof("enabling single-node mode")
		config.EnableSingleNode = true
		config.DisableBootstrapAfterElect = false
	}

	// Create the snapshot store. This allows the Raft to truncate the log.
	snapshots, err := raft.NewFileSnapshotStore(store.raftDir, retainSnapshotCount, os.Stderr)
	//snapshots, err := NewRelSnapshotStore(retainSnapshotCount, os.Stderr)
	if err != nil {
		return log.Errorf("file snapshot store: %s", err)
	}
	//	snapshots := raft.NewDiscardSnapshotStore()

	// Create the log store and stable store.
	logStore := NewRelationalStore()
	log.Debugf("raft: logStore=%+v", logStore)
	if snapshotsList, err := snapshots.List(); err == nil && len(snapshotsList) == 0 {
		log.Debugf("raft: cannot find any snapshots.")
		if firstIndex, _ := logStore.FirstIndex(); firstIndex > 1 {
			log.Debugf("raft: log store first index is %+v. Since no spanshots exist I will purge the log store", firstIndex)
			logStore.DeleteAll()
		}
	}

	// Instantiate the Raft systems.
	if store.raft, err = raft.NewRaft(config, (*fsm)(store), logStore, logStore, snapshots, peerStore, transport); err != nil {
		return fmt.Errorf("error creating new raft: %s", err)
	}
	store.peerStore = peerStore
	log.Infof("new raft created")

	return nil
}

// Join joins a node, located at addr, to this store. The node must be ready to
// respond to Raft communications at that address.
func (store *Store) Join(addr string) error {
	log.Infof("received join request for remote node as %s", addr)

	f := store.raft.AddPeer(addr)
	if f.Error() != nil {
		return f.Error()
	}
	log.Infof("node at %s joined successfully", addr)
	return nil
}

// genericCommand requests consensus for applying a single command.
// This is an internal orchestrator implementation
func (store *Store) genericCommand(op string, bytes []byte) (response interface{}, err error) {
	if store.raft.State() != raft.Leader {
		return nil, fmt.Errorf("not leader")
	}

	b, err := json.Marshal(&storeCommand{Op: op, Value: bytes})
	if err != nil {
		return nil, err
	}

	f := store.raft.Apply(b, raftTimeout)
	if err = f.Error(); err != nil {
		return nil, err
	}
	r := f.Response()
	return r, nil
}
<commit_msg>reasonable snapshot interval time<commit_after>package orcraft

import (
	"encoding/json"
	"fmt"
	"net"
	"os"
	"strings"
	"time"

	"github.com/openark/golib/log"

	"github.com/hashicorp/raft"
)

const (
	retainSnapshotCount = 288
	snapshotInterval    = 10 * time.Minute
	raftTimeout         = 10 * time.Second
)

type Store struct {
	raftDir  string
	raftBind string

	raft      *raft.Raft // The consensus mechanism
	peerStore raft.PeerStore

	applier                CommandApplier
	snapshotCreatorApplier SnapshotCreatorApplier
}

type storeCommand struct {
	Op    string `json:"op,omitempty"`
	Value []byte `json:"value,omitempty"`
}

// NewStore inits and returns a new store
func NewStore(raftDir string, raftBind string, applier CommandApplier, snapshotCreatorApplier SnapshotCreatorApplier) *Store {
	return &Store{
		raftDir:                raftDir,
		raftBind:               raftBind,
		applier:                applier,
		snapshotCreatorApplier: snapshotCreatorApplier,
	}
}

// Open opens the store. If enableSingle is set, and there are no existing peers,
// then this node becomes the first node, and therefore leader, of the cluster.
func (store *Store) Open(peerNodes []string) error {
	// Setup Raft configuration.
	config := raft.DefaultConfig()
	config.SnapshotThreshold = 1
	config.SnapshotInterval = snapshotInterval

	// Setup Raft communication.
	addr, err := net.ResolveTCPAddr("tcp", store.raftBind)
	if err != nil {
		return err
	}
	log.Debugf("raft: addr=%+v", addr)
	transport, err := raft.NewTCPTransport(store.raftBind, addr, 3, 10*time.Second, os.Stderr)
	if err != nil {
		return err
	}
	log.Debugf("raft: transport=%+v", transport)

	peers := make([]string, 0, 10)
	for _, peerNode := range peerNodes {
		peerNode = strings.TrimSpace(peerNode)
		peers = raft.AddUniquePeer(peers, peerNode)
	}
	log.Debugf("raft: peers=%+v", peers)

	// Create peer storage.
	peerStore := &raft.StaticPeers{}
	if err := peerStore.SetPeers(peers); err != nil {
		return err
	}

	// Allow the node to enter single-mode, potentially electing itself, if
	// explicitly enabled and there is only 1 node in the cluster already.
	if len(peerNodes) == 0 && len(peers) <= 1 {
		log.Infof("enabling single-node mode")
		config.EnableSingleNode = true
		config.DisableBootstrapAfterElect = false
	}

	// Create the snapshot store. This allows the Raft to truncate the log.
	snapshots, err := raft.NewFileSnapshotStore(store.raftDir, retainSnapshotCount, os.Stderr)
	//snapshots, err := NewRelSnapshotStore(retainSnapshotCount, os.Stderr)
	if err != nil {
		return log.Errorf("file snapshot store: %s", err)
	}
	//	snapshots := raft.NewDiscardSnapshotStore()

	// Create the log store and stable store.
	logStore := NewRelationalStore()
	log.Debugf("raft: logStore=%+v", logStore)
	if snapshotsList, err := snapshots.List(); err == nil && len(snapshotsList) == 0 {
		log.Debugf("raft: cannot find any snapshots.")
		if firstIndex, _ := logStore.FirstIndex(); firstIndex > 1 {
			log.Debugf("raft: log store first index is %+v. Since no spanshots exist I will purge the log store", firstIndex)
			logStore.DeleteAll()
		}
	}

	// Instantiate the Raft systems.
	if store.raft, err = raft.NewRaft(config, (*fsm)(store), logStore, logStore, snapshots, peerStore, transport); err != nil {
		return fmt.Errorf("error creating new raft: %s", err)
	}
	store.peerStore = peerStore
	log.Infof("new raft created")

	return nil
}

// Join joins a node, located at addr, to this store. The node must be ready to
// respond to Raft communications at that address.
func (store *Store) Join(addr string) error {
	log.Infof("received join request for remote node as %s", addr)

	f := store.raft.AddPeer(addr)
	if f.Error() != nil {
		return f.Error()
	}
	log.Infof("node at %s joined successfully", addr)
	return nil
}

// genericCommand requests consensus for applying a single command.
// This is an internal orchestrator implementation
func (store *Store) genericCommand(op string, bytes []byte) (response interface{}, err error) {
	if store.raft.State() != raft.Leader {
		return nil, fmt.Errorf("not leader")
	}

	b, err := json.Marshal(&storeCommand{Op: op, Value: bytes})
	if err != nil {
		return nil, err
	}

	f := store.raft.Apply(b, raftTimeout)
	if err = f.Error(); err != nil {
		return nil, err
	}
	r := f.Response()
	return r, nil
}
<|endoftext|>


<commit_before>package teams

import (
	"errors"
	"fmt"

	"golang.org/x/net/context"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol/keybase1"
)

type Team struct {
	libkb.Contextified

	Name           string
	Chain          *TeamSigChainState
	Box            TeamBox
	ReaderKeyMasks []keybase1.ReaderKeyMask

	secret        []byte
	signingKey    libkb.NaclSigningKeyPair
	encryptionKey libkb.NaclDHKeyPair

	me *libkb.User
}

func NewTeam(g *libkb.GlobalContext, name string) *Team {
	return &Team{Name: name, Contextified: libkb.NewContextified(g)}
}

func (t *Team) SharedSecret(ctx context.Context) ([]byte, error) {
	if t.secret == nil {
		userEncKey, err := t.perUserEncryptionKey(ctx)
		if err != nil {
			return nil, err
		}

		secret, err := t.Box.Open(userEncKey)
		if err != nil {
			return nil, err
		}

		signingKey, encryptionKey, err := generatePerTeamKeysFromSecret(secret)
		if err != nil {
			return nil, err
		}

		teamKey, err := t.Chain.GetPerTeamKeyAtGeneration(t.Box.Generation)
		if err != nil {
			return nil, err
		}

		if !teamKey.SigKID.Equal(signingKey.GetKID()) {
			return nil, errors.New("derived signing key did not match key in team chain")
		}

		if !teamKey.EncKID.Equal(encryptionKey.GetKID()) {
			return nil, errors.New("derived encryption key did not match key in team chain")
		}

		t.secret = secret
		t.signingKey = signingKey
		t.encryptionKey = encryptionKey
	}

	return t.secret, nil
}

func (t *Team) KBFSKey(ctx context.Context) (keybase1.TeamApplicationKey, error) {
	return t.ApplicationKey(ctx, keybase1.TeamApplication_KBFS)
}

func (t *Team) ChatKey(ctx context.Context) (keybase1.TeamApplicationKey, error) {
	return t.ApplicationKey(ctx, keybase1.TeamApplication_CHAT)
}

func (t *Team) UsernamesWithRole(role keybase1.TeamRole) ([]libkb.NormalizedUsername, error) {
	uvs, err := t.Chain.GetUsersWithRole(role)
	if err != nil {
		return nil, err
	}
	names := make([]libkb.NormalizedUsername, len(uvs))
	for i, uv := range uvs {
		names[i] = uv.Username
	}
	return names, nil
}

func (t *Team) Members() (keybase1.TeamMembers, error) {
	var members keybase1.TeamMembers

	x, err := t.UsernamesWithRole(keybase1.TeamRole_OWNER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Owners = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_ADMIN)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Admins = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_WRITER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Writers = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_READER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Readers = libkb.NormalizedUsernamesToStrings(x)

	return members, nil
}

func (t *Team) perUserEncryptionKey(ctx context.Context) (*libkb.NaclDHKeyPair, error) {
	// TeamBox has PerUserKeySeqno but libkb.PerUserKeyring has no seqnos.
	// ComputedKeyInfos does, though, so let's find the key there first, then
	// look for it in libkb.PerUserKeyring.

	me, err := t.loadMe()
	if err != nil {
		return nil, err
	}

	cki := me.GetComputedKeyInfos()
	if cki == nil {
		return nil, errors.New("no computed key infos for self")
	}

	var encKID keybase1.KID
	for _, key := range cki.PerUserKeys {
		if key.Seqno == t.Box.PerUserKeySeqno {
			encKID = key.EncKID
			break
		}
	}
	if encKID.IsNil() {
		return nil, libkb.NotFoundError{Msg: fmt.Sprintf("per-user-key not found seqno=%d", t.Box.PerUserKeySeqno)}
	}

	kr, err := t.G().GetPerUserKeyring()
	if err != nil {
		return nil, err
	}
	// XXX this seems to be necessary:
	if err := kr.Sync(ctx); err != nil {
		return nil, err
	}
	encKey, err := kr.GetEncryptionKeyByKID(ctx, encKID)
	if err != nil {
		return nil, err
	}
	if encKey.Private == nil {
		return nil, errors.New("per user enckey is locked")
	}

	return encKey, nil
}

func (t *Team) NextSeqno() keybase1.Seqno {
	return t.Chain.GetLatestSeqno() + 1
}

// ApplicationKey returns the most recent key for an application.
func (t *Team) ApplicationKey(ctx context.Context, application keybase1.TeamApplication) (keybase1.TeamApplicationKey, error) {
	secret, err := t.SharedSecret(ctx)
	if err != nil {
		return keybase1.TeamApplicationKey{}, err
	}

	var max keybase1.ReaderKeyMask
	for _, rkm := range t.ReaderKeyMasks {
		if keybase1.TeamApplication(rkm.Application) != application {
			continue
		}
		if rkm.Generation < max.Generation {
			continue
		}
		max = rkm
	}

	if max.Application == 0 {
		return keybase1.TeamApplicationKey{}, libkb.NotFoundError{Msg: fmt.Sprintf("no mask found for application %d", application)}
	}

	return t.applicationKeyForMask(max, secret)
}

func (t *Team) ApplicationKeyAtGeneration(application keybase1.TeamApplication, generation int, secret []byte) (keybase1.TeamApplicationKey, error) {
	for _, rkm := range t.ReaderKeyMasks {
		if keybase1.TeamApplication(rkm.Application) != application {
			continue
		}
		if rkm.Generation != generation {
			continue
		}
		return t.applicationKeyForMask(rkm, secret)
	}

	return keybase1.TeamApplicationKey{}, libkb.NotFoundError{Msg: fmt.Sprintf("no mask found for application %d, generation %d", application, generation)}
}

func (t *Team) applicationKeyForMask(mask keybase1.ReaderKeyMask, secret []byte) (keybase1.TeamApplicationKey, error) {
	var derivationString string
	switch keybase1.TeamApplication(mask.Application) {
	case keybase1.TeamApplication_KBFS:
		derivationString = libkb.TeamKBFSDerivationString
	case keybase1.TeamApplication_CHAT:
		derivationString = libkb.TeamChatDerivationString
	case keybase1.TeamApplication_SALTPACK:
		derivationString = libkb.TeamSaltpackDerivationString
	default:
		return keybase1.TeamApplicationKey{}, errors.New("invalid application id")
	}

	key := keybase1.TeamApplicationKey{
		Application: keybase1.TeamApplication(mask.Application),
		Generation:  mask.Generation,
	}

	if len(mask.Mask) != 32 {
		return keybase1.TeamApplicationKey{}, fmt.Errorf("mask length: %d, expected 32", len(mask.Mask))
	}

	secBytes := make([]byte, len(mask.Mask))
	n := libkb.XORBytes(secBytes, derivedSecret(secret, derivationString), mask.Mask)
	if n != 32 {
		return key, errors.New("invalid derived secret xor mask size")
	}
	copy(key.Key[:], secBytes)

	return key, nil
}

type ChangeReq struct {
	Owners  []string
	Admins  []string
	Writers []string
	Readers []string
	None    []string
}

func (t *Team) ChangeMembership(ctx context.Context, req ChangeReq) error {
	// make keys for the team
	if _, err := t.SharedSecret(ctx); err != nil {
		return err
	}

	// load the member set specified in req
	memSet, err := newMemberSet(ctx, t.G(), req)
	if err != nil {
		return err
	}

	// create the team section of the signature
	section, err := memSet.Section(t.Chain.GetID())
	if err != nil {
		return err
	}

	// create the change item
	sigMultiItem, err := t.sigChangeItem(section)
	if err != nil {
		return err
	}

	t.G().Log.Warning("sigMultiItem: %s", sigMultiItem)

	// create secret boxes for recipients
	secretBoxes, err := t.recipientBoxes(memSet)
	if err != nil {
		return err
	}

	// make the payload
	payload := t.sigPayload(sigMultiItem, secretBoxes)

	// send it to the server
	return t.postMulti(payload)
}

func (t *Team) loadMe() (*libkb.User, error) {
	if t.me == nil {
		me, err := libkb.LoadMe(libkb.NewLoadUserArg(t.G()))
		if err != nil {
			return nil, err
		}
		t.me = me
	}

	return t.me, nil
}

func (t *Team) sigChangeItem(section SCTeamSection) (libkb.SigMultiItem, error) {
	me, err := t.loadMe()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}
	deviceSigningKey, err := t.G().ActiveDevice.SigningKey()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}
	sig, err := ChangeMembershipSig(me, t.Chain.GetLatestLinkID(), t.NextSeqno(), deviceSigningKey, section)
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	sigJSON, err := sig.Marshal()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	v2Sig, err := makeSigchainV2OuterSig(
		deviceSigningKey,
		libkb.LinkTypeChangeMembership,
		t.NextSeqno(),
		sigJSON,
		t.Chain.GetLatestLinkID(),
		false, /* hasRevokes */
	)
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	sigMultiItem := libkb.SigMultiItem{
		Sig:        v2Sig,
		SigningKID: deviceSigningKey.GetKID(),
		Type:       string(libkb.LinkTypeChangeMembership),
		SigInner:   string(sigJSON),
		TeamID:     t.Chain.GetID(),
		PublicKeys: &libkb.SigMultiItemPublicKeys{
			Encryption: t.encryptionKey.GetKID(),
			Signing:    t.signingKey.GetKID(),
		},
	}
	return sigMultiItem, nil
}

func (t *Team) recipientBoxes(memSet *memberSet) (*PerTeamSharedSecretBoxes, error) {
	deviceEncryptionKey, err := t.G().ActiveDevice.EncryptionKey()
	if err != nil {
		return nil, err
	}
	return boxTeamSharedSecret(t.secret, deviceEncryptionKey, memSet.recipients)
}

func (t *Team) sigPayload(sigMultiItem libkb.SigMultiItem, secretBoxes *PerTeamSharedSecretBoxes) libkb.JSONPayload {
	payload := make(libkb.JSONPayload)
	payload["sigs"] = []interface{}{sigMultiItem}
	payload["per_team_key"] = secretBoxes
	return payload
}

func (t *Team) postMulti(payload libkb.JSONPayload) error {
	_, err := t.G().API.PostJSON(libkb.APIArg{
		Endpoint:    "sig/multi",
		SessionType: libkb.APISessionTypeREQUIRED,
		JSONPayload: payload,
	})
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>Remove some casts<commit_after>package teams

import (
	"errors"
	"fmt"

	"golang.org/x/net/context"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol/keybase1"
)

type Team struct {
	libkb.Contextified

	Name           string
	Chain          *TeamSigChainState
	Box            TeamBox
	ReaderKeyMasks []keybase1.ReaderKeyMask

	secret        []byte
	signingKey    libkb.NaclSigningKeyPair
	encryptionKey libkb.NaclDHKeyPair

	me *libkb.User
}

func NewTeam(g *libkb.GlobalContext, name string) *Team {
	return &Team{Name: name, Contextified: libkb.NewContextified(g)}
}

func (t *Team) SharedSecret(ctx context.Context) ([]byte, error) {
	if t.secret == nil {
		userEncKey, err := t.perUserEncryptionKey(ctx)
		if err != nil {
			return nil, err
		}

		secret, err := t.Box.Open(userEncKey)
		if err != nil {
			return nil, err
		}

		signingKey, encryptionKey, err := generatePerTeamKeysFromSecret(secret)
		if err != nil {
			return nil, err
		}

		teamKey, err := t.Chain.GetPerTeamKeyAtGeneration(t.Box.Generation)
		if err != nil {
			return nil, err
		}

		if !teamKey.SigKID.Equal(signingKey.GetKID()) {
			return nil, errors.New("derived signing key did not match key in team chain")
		}

		if !teamKey.EncKID.Equal(encryptionKey.GetKID()) {
			return nil, errors.New("derived encryption key did not match key in team chain")
		}

		t.secret = secret
		t.signingKey = signingKey
		t.encryptionKey = encryptionKey
	}

	return t.secret, nil
}

func (t *Team) KBFSKey(ctx context.Context) (keybase1.TeamApplicationKey, error) {
	return t.ApplicationKey(ctx, keybase1.TeamApplication_KBFS)
}

func (t *Team) ChatKey(ctx context.Context) (keybase1.TeamApplicationKey, error) {
	return t.ApplicationKey(ctx, keybase1.TeamApplication_CHAT)
}

func (t *Team) UsernamesWithRole(role keybase1.TeamRole) ([]libkb.NormalizedUsername, error) {
	uvs, err := t.Chain.GetUsersWithRole(role)
	if err != nil {
		return nil, err
	}
	names := make([]libkb.NormalizedUsername, len(uvs))
	for i, uv := range uvs {
		names[i] = uv.Username
	}
	return names, nil
}

func (t *Team) Members() (keybase1.TeamMembers, error) {
	var members keybase1.TeamMembers

	x, err := t.UsernamesWithRole(keybase1.TeamRole_OWNER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Owners = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_ADMIN)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Admins = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_WRITER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Writers = libkb.NormalizedUsernamesToStrings(x)

	x, err = t.UsernamesWithRole(keybase1.TeamRole_READER)
	if err != nil {
		return keybase1.TeamMembers{}, err
	}
	members.Readers = libkb.NormalizedUsernamesToStrings(x)

	return members, nil
}

func (t *Team) perUserEncryptionKey(ctx context.Context) (*libkb.NaclDHKeyPair, error) {
	// TeamBox has PerUserKeySeqno but libkb.PerUserKeyring has no seqnos.
	// ComputedKeyInfos does, though, so let's find the key there first, then
	// look for it in libkb.PerUserKeyring.

	me, err := t.loadMe()
	if err != nil {
		return nil, err
	}

	cki := me.GetComputedKeyInfos()
	if cki == nil {
		return nil, errors.New("no computed key infos for self")
	}

	var encKID keybase1.KID
	for _, key := range cki.PerUserKeys {
		if key.Seqno == t.Box.PerUserKeySeqno {
			encKID = key.EncKID
			break
		}
	}
	if encKID.IsNil() {
		return nil, libkb.NotFoundError{Msg: fmt.Sprintf("per-user-key not found seqno=%d", t.Box.PerUserKeySeqno)}
	}

	kr, err := t.G().GetPerUserKeyring()
	if err != nil {
		return nil, err
	}
	// XXX this seems to be necessary:
	if err := kr.Sync(ctx); err != nil {
		return nil, err
	}
	encKey, err := kr.GetEncryptionKeyByKID(ctx, encKID)
	if err != nil {
		return nil, err
	}
	if encKey.Private == nil {
		return nil, errors.New("per user enckey is locked")
	}

	return encKey, nil
}

func (t *Team) NextSeqno() keybase1.Seqno {
	return t.Chain.GetLatestSeqno() + 1
}

// ApplicationKey returns the most recent key for an application.
func (t *Team) ApplicationKey(ctx context.Context, application keybase1.TeamApplication) (keybase1.TeamApplicationKey, error) {
	secret, err := t.SharedSecret(ctx)
	if err != nil {
		return keybase1.TeamApplicationKey{}, err
	}

	var max keybase1.ReaderKeyMask
	for _, rkm := range t.ReaderKeyMasks {
		if rkm.Application != application {
			continue
		}
		if rkm.Generation < max.Generation {
			continue
		}
		max = rkm
	}

	if max.Application == 0 {
		return keybase1.TeamApplicationKey{}, libkb.NotFoundError{Msg: fmt.Sprintf("no mask found for application %d", application)}
	}

	return t.applicationKeyForMask(max, secret)
}

func (t *Team) ApplicationKeyAtGeneration(application keybase1.TeamApplication, generation int, secret []byte) (keybase1.TeamApplicationKey, error) {
	for _, rkm := range t.ReaderKeyMasks {
		if rkm.Application != application {
			continue
		}
		if rkm.Generation != generation {
			continue
		}
		return t.applicationKeyForMask(rkm, secret)
	}

	return keybase1.TeamApplicationKey{}, libkb.NotFoundError{Msg: fmt.Sprintf("no mask found for application %d, generation %d", application, generation)}
}

func (t *Team) applicationKeyForMask(mask keybase1.ReaderKeyMask, secret []byte) (keybase1.TeamApplicationKey, error) {
	var derivationString string
	switch mask.Application {
	case keybase1.TeamApplication_KBFS:
		derivationString = libkb.TeamKBFSDerivationString
	case keybase1.TeamApplication_CHAT:
		derivationString = libkb.TeamChatDerivationString
	case keybase1.TeamApplication_SALTPACK:
		derivationString = libkb.TeamSaltpackDerivationString
	default:
		return keybase1.TeamApplicationKey{}, errors.New("invalid application id")
	}

	key := keybase1.TeamApplicationKey{
		Application: mask.Application,
		Generation:  mask.Generation,
	}

	if len(mask.Mask) != 32 {
		return keybase1.TeamApplicationKey{}, fmt.Errorf("mask length: %d, expected 32", len(mask.Mask))
	}

	secBytes := make([]byte, len(mask.Mask))
	n := libkb.XORBytes(secBytes, derivedSecret(secret, derivationString), mask.Mask)
	if n != 32 {
		return key, errors.New("invalid derived secret xor mask size")
	}
	copy(key.Key[:], secBytes)

	return key, nil
}

type ChangeReq struct {
	Owners  []string
	Admins  []string
	Writers []string
	Readers []string
	None    []string
}

func (t *Team) ChangeMembership(ctx context.Context, req ChangeReq) error {
	// make keys for the team
	if _, err := t.SharedSecret(ctx); err != nil {
		return err
	}

	// load the member set specified in req
	memSet, err := newMemberSet(ctx, t.G(), req)
	if err != nil {
		return err
	}

	// create the team section of the signature
	section, err := memSet.Section(t.Chain.GetID())
	if err != nil {
		return err
	}

	// create the change item
	sigMultiItem, err := t.sigChangeItem(section)
	if err != nil {
		return err
	}

	t.G().Log.Warning("sigMultiItem: %s", sigMultiItem)

	// create secret boxes for recipients
	secretBoxes, err := t.recipientBoxes(memSet)
	if err != nil {
		return err
	}

	// make the payload
	payload := t.sigPayload(sigMultiItem, secretBoxes)

	// send it to the server
	return t.postMulti(payload)
}

func (t *Team) loadMe() (*libkb.User, error) {
	if t.me == nil {
		me, err := libkb.LoadMe(libkb.NewLoadUserArg(t.G()))
		if err != nil {
			return nil, err
		}
		t.me = me
	}

	return t.me, nil
}

func (t *Team) sigChangeItem(section SCTeamSection) (libkb.SigMultiItem, error) {
	me, err := t.loadMe()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}
	deviceSigningKey, err := t.G().ActiveDevice.SigningKey()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}
	sig, err := ChangeMembershipSig(me, t.Chain.GetLatestLinkID(), t.NextSeqno(), deviceSigningKey, section)
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	sigJSON, err := sig.Marshal()
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	v2Sig, err := makeSigchainV2OuterSig(
		deviceSigningKey,
		libkb.LinkTypeChangeMembership,
		t.NextSeqno(),
		sigJSON,
		t.Chain.GetLatestLinkID(),
		false, /* hasRevokes */
	)
	if err != nil {
		return libkb.SigMultiItem{}, err
	}

	sigMultiItem := libkb.SigMultiItem{
		Sig:        v2Sig,
		SigningKID: deviceSigningKey.GetKID(),
		Type:       string(libkb.LinkTypeChangeMembership),
		SigInner:   string(sigJSON),
		TeamID:     t.Chain.GetID(),
		PublicKeys: &libkb.SigMultiItemPublicKeys{
			Encryption: t.encryptionKey.GetKID(),
			Signing:    t.signingKey.GetKID(),
		},
	}
	return sigMultiItem, nil
}

func (t *Team) recipientBoxes(memSet *memberSet) (*PerTeamSharedSecretBoxes, error) {
	deviceEncryptionKey, err := t.G().ActiveDevice.EncryptionKey()
	if err != nil {
		return nil, err
	}
	return boxTeamSharedSecret(t.secret, deviceEncryptionKey, memSet.recipients)
}

func (t *Team) sigPayload(sigMultiItem libkb.SigMultiItem, secretBoxes *PerTeamSharedSecretBoxes) libkb.JSONPayload {
	payload := make(libkb.JSONPayload)
	payload["sigs"] = []interface{}{sigMultiItem}
	payload["per_team_key"] = secretBoxes
	return payload
}

func (t *Team) postMulti(payload libkb.JSONPayload) error {
	_, err := t.G().API.PostJSON(libkb.APIArg{
		Endpoint:    "sig/multi",
		SessionType: libkb.APISessionTypeREQUIRED,
		JSONPayload: payload,
	})
	if err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>// Copyright 2010  The "goheader" Authors
//
// Use of this source code is governed by the Simplified BSD License
// that can be found in the LICENSE file.
//
// This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied. See the License
// for more details.

package main

import (
	"bufio"
	//"bytes"
	"container/vector"
	"flag"
	"fmt"
	"os"
	"path"
	"regexp"
	"strings"
)

const COMMENT_C_LINE = "//!!! "

var goHeader = `// {cmd}
// MACHINE GENERATED.

package {pkg}
`

// Flags
var (
	fOS      = flag.String("s", "", "The operating system")
	fPackage = flag.String("p", "", "The name of the package")
	fListOS  = flag.Bool("ls", false, "List of valid systems")
)

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: goheader -s system -g package [defs.h...]\n")
	flag.PrintDefaults()
	os.Exit(1)
}

// Translates C type declaration into Go type declaration.
func translateC(fname string) os.Error {
	// === Regular expressions
	reSkip := regexp.MustCompile(`^(\n|//)`) // Empty lines and comments.

	reType := regexp.MustCompile(`^(typedef)[ \t]+(.+)[ \t]+(.+)[;](.+)?`)

	reStruct := regexp.MustCompile(`^(struct)[ \t]+(.+)[ \t]*{`)
	reStructField := regexp.MustCompile(`^[ \t]*(.+)[ \t]+(.+)[;](.+)?`)
	reStructFieldName := regexp.MustCompile(`^([^_]*_)?(.+)`)

	reDefine := regexp.MustCompile(`^[ \t]*#[ \t]*(define|DEFINE)[ \t]+([^ \t]+)[ \t]+(.+)`)
	reDefineMacro := regexp.MustCompile(`^.*(\(.*\))`)

	reSingleComment := regexp.MustCompile(`^(.+)?/\*[ \t]*(.+)[ \t]*\*/`)
	reStartMultipleComment := regexp.MustCompile(`^/\*(.+)?`)
	reMiddleMultipleComment := regexp.MustCompile(`^[ \t*]*(.+)`)
	reEndMultipleComment := regexp.MustCompile(`^(.+)?\*/`)

	// === File to read
	var isMultipleComment, isDefine, isStruct bool
	var extraType vector.StringVector // Types defined in C header.

	file, err := os.Open(fname, os.O_RDONLY, 0)
	if err != nil {
		return err
	}
	defer file.Close()

	r := bufio.NewReader(file)

	fmt.Println(goHeader)

	for {
		line, err := r.ReadString('\n')
		if err == os.EOF {
			break
		}
		line = strings.TrimSpace(line) + "\n"
		isSingleComment := false

		// === Translate comment of single line.
		if !isMultipleComment {
			if sub := reSingleComment.FindStringSubmatch(line); sub != nil {
				isSingleComment = true
				line = "// " + sub[2] + "\n"

				if sub[1] != "" {
					line = sub[1] + line
				}
			}
		}
		if !isSingleComment && !isMultipleComment && strings.HasPrefix(line, "/*") {
			isMultipleComment = true
		}

		// === Translate comments of multiple line.
		if isMultipleComment {
			if sub := reStartMultipleComment.FindStringSubmatch(line); sub != nil {
				if sub[1] != "\n" {
					line = "// " + sub[1]
					fmt.Print(line)
				}
				continue
			}
			if sub := reEndMultipleComment.FindStringSubmatch(line); sub != nil {
				if sub[1] != "" {
					line = "// " + sub[1] + "\n"
					fmt.Print(line)
				}
				isMultipleComment = false
				continue
			}
			if sub := reMiddleMultipleComment.FindStringSubmatch(line); sub != nil {
				line = "// " + sub[1]
				fmt.Print(line)
				continue
			}
		}

		// === Translate type definitions.
		if sub := reType.FindStringSubmatch(line); sub != nil {
			// Add the new type.
			extraType.Push(sub[3])

			gotype, ok := ctypeTogo(sub[2], &extraType)
			line = fmt.Sprintf("type %s %s", sub[3], gotype)

			if sub[4] != "\n" {
				line += sub[4]
			} else {
				line += "\n"
			}
			if !ok {
				line = COMMENT_C_LINE + line
			}

			fmt.Print(line)
			continue
		}

		// === Translate defines.
		if sub := reDefine.FindStringSubmatch(line); sub != nil {
			if !isDefine {
				isDefine = true
				fmt.Print("const (\n")
			}
			line = fmt.Sprintf("%s = %s", sub[2], sub[3])

			// Removes comment (if any) to ckeck if it is a macro.
			lastField := strings.Split(sub[3], "//", -1)[0]
			if reDefineMacro.MatchString(lastField) {
				line = COMMENT_C_LINE + line
			}

			fmt.Print(line)
			continue
		}
		if isDefine && line == "\n" {
			fmt.Print(")\n\n")
			isDefine = false
			continue
		}

		// === Translate structs.
		if !isStruct {
			if sub := reStruct.FindStringSubmatch(line); sub != nil {
				isStruct = true

				if isDefine {
					fmt.Print(")\n")
					isDefine = false
				}

				fmt.Printf("type %s struct {\n", strings.Title(sub[2]))
				continue
			}
		} else {
			if sub := reStructField.FindStringSubmatch(line); sub != nil {
				// Translate the field type.
				gotype, ok := ctypeTogo(sub[1], &extraType)

				// === Translate the field name.
				fieldName := reStructFieldName.FindStringSubmatch(sub[2])
				_fieldName := ""

				if fieldName[1] != "" {
					_fieldName = fieldName[2]
				} else {
					_fieldName = fieldName[0]
				}
				// ===

				line = fmt.Sprintf("%s %s %s",
					strings.Title(_fieldName), gotype, sub[3])

				// C type not found.
				if !ok {
					line = COMMENT_C_LINE + line
				}

				fmt.Print(line)
				continue
			}
			if strings.HasPrefix(line, "}") {
				fmt.Print(strings.Replace(line, ";", "", 1))
				isStruct = false
				continue
			}
		}

		// Comment another C lines.
		//if line != "\n" && !strings.HasPrefix(line, "//") {
		if !reSkip.MatchString(line) {
			line = COMMENT_C_LINE + line
		}

		fmt.Print(line)
	}

	return nil
}

// Translates a C type definition into Go definition. The C header could have
// defined new types so they're checked in the firs place.
func ctypeTogo(ctype string, extraCtype *vector.StringVector) (gotype string, ok bool) {
	for _, v := range *extraCtype {
		if v == ctype {
			return ctype, true
		}
	}

	switch ctype {
	case "char", "signed char", "signed short int", "short int", "short":
		return "int8", true
	case "unsigned char", "unsigned short int", "unsigned short":
		return "uint8", true
	case "int", "signed int":
		return "int16", true
	case "unsigned int", "unsigned":
		return "uint16", true
	case "signed long int", "long int", "long":
		return "int32", true
	case "unsigned long int", "unsigned long":
		return "uint32", true
	case "float":
		return "float32", true
	case "double", "long double":
		return "float64", true
	}
	return ctype, false
}

func main() {
	var isValidOS bool
	validOS := []string{"darwin", "freebsd", "linux", "windows"}

	// === Parse the flags
	flag.Usage = usage
	flag.Parse()

	if *fListOS {
		fmt.Print("  = Systems\n\n  ")
		fmt.Println(validOS)
		os.Exit(0)
	}
	if len(os.Args) == 1 || *fOS == "" || *fPackage == "" {
		usage()
	}

	*fOS = strings.ToLower(*fOS)

	for _, v := range validOS {
		if v == *fOS {
			isValidOS = true
			break
		}
	}
	if !isValidOS {
		fmt.Fprintf(os.Stderr, "ERROR: System passed in flag 's' is invalid\n")
		os.Exit(1)
	}

	// === Update header
	cmd := strings.Join(os.Args, " ")
	goHeader = strings.Replace(goHeader, "{cmd}", path.Base(cmd), 1)
	goHeader = strings.Replace(goHeader, "{pkg}", *fPackage, 1)

File := "../test/header.h"

	if err := translateC(File); err != nil {
		fmt.Fprintf(os.Stderr, err.String())
		os.Exit(1)
	}
}

<commit_msg>Renamed any variables.<commit_after>// Copyright 2010  The "goheader" Authors
//
// Use of this source code is governed by the Simplified BSD License
// that can be found in the LICENSE file.
//
// This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
// OR CONDITIONS OF ANY KIND, either express or implied. See the License
// for more details.

package main

import (
	"bufio"
	//"bytes"
	"container/vector"
	"flag"
	"fmt"
	"os"
	"path"
	"regexp"
	"strings"
)

const COMMENT_C_LINE = "//!!! "

var goHeader = `// {cmd}
// MACHINE GENERATED.

package {pkg}
`

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: goheader -s system -g package [defs.h...]\n")
	flag.PrintDefaults()
	os.Exit(1)
}

// Translates C type declaration into Go type declaration.
func translateC(fname string) os.Error {
	// === Regular expressions
	reSkip := regexp.MustCompile(`^(\n|//)`) // Empty lines and comments.

	reType := regexp.MustCompile(`^(typedef)[ \t]+(.+)[ \t]+(.+)[;](.+)?`)

	reStruct := regexp.MustCompile(`^(struct)[ \t]+(.+)[ \t]*{`)
	reStructField := regexp.MustCompile(`^[ \t]*(.+)[ \t]+(.+)[;](.+)?`)
	reStructFieldName := regexp.MustCompile(`^([^_]*_)?(.+)`)

	reDefine := regexp.MustCompile(`^[ \t]*#[ \t]*(define|DEFINE)[ \t]+([^ \t]+)[ \t]+(.+)`)
	reDefineMacro := regexp.MustCompile(`^.*(\(.*\))`)

	reSingleComment := regexp.MustCompile(`^(.+)?/\*[ \t]*(.+)[ \t]*\*/`)
	reStartMultipleComment := regexp.MustCompile(`^/\*(.+)?`)
	reMiddleMultipleComment := regexp.MustCompile(`^[ \t*]*(.+)`)
	reEndMultipleComment := regexp.MustCompile(`^(.+)?\*/`)

	// === File to read
	var isMultipleComment, isDefine, isStruct bool
	var extraType vector.StringVector // Types defined in C header.

	file, err := os.Open(fname, os.O_RDONLY, 0)
	if err != nil {
		return err
	}
	defer file.Close()

	r := bufio.NewReader(file)

	fmt.Println(goHeader)

	for {
		line, err := r.ReadString('\n')
		if err == os.EOF {
			break
		}
		line = strings.TrimSpace(line) + "\n"
		isSingleComment := false

		// === Translate comment of single line.
		if !isMultipleComment {
			if sub := reSingleComment.FindStringSubmatch(line); sub != nil {
				isSingleComment = true
				line = "// " + sub[2] + "\n"

				if sub[1] != "" {
					line = sub[1] + line
				}
			}
		}
		if !isSingleComment && !isMultipleComment && strings.HasPrefix(line, "/*") {
			isMultipleComment = true
		}

		// === Translate comments of multiple line.
		if isMultipleComment {
			if sub := reStartMultipleComment.FindStringSubmatch(line); sub != nil {
				if sub[1] != "\n" {
					line = "// " + sub[1]
					fmt.Print(line)
				}
				continue
			}
			if sub := reEndMultipleComment.FindStringSubmatch(line); sub != nil {
				if sub[1] != "" {
					line = "// " + sub[1] + "\n"
					fmt.Print(line)
				}
				isMultipleComment = false
				continue
			}
			if sub := reMiddleMultipleComment.FindStringSubmatch(line); sub != nil {
				line = "// " + sub[1]
				fmt.Print(line)
				continue
			}
		}

		// === Translate type definitions.
		if sub := reType.FindStringSubmatch(line); sub != nil {
			// Add the new type.
			extraType.Push(sub[3])

			gotype, ok := ctypeTogo(sub[2], &extraType)
			line = fmt.Sprintf("type %s %s", sub[3], gotype)

			if sub[4] != "\n" {
				line += sub[4]
			} else {
				line += "\n"
			}
			if !ok {
				line = COMMENT_C_LINE + line
			}

			fmt.Print(line)
			continue
		}

		// === Translate defines.
		if sub := reDefine.FindStringSubmatch(line); sub != nil {
			if !isDefine {
				isDefine = true
				fmt.Print("const (\n")
			}
			line = fmt.Sprintf("%s = %s", sub[2], sub[3])

			// Removes comment (if any) to ckeck if it is a macro.
			lastField := strings.Split(sub[3], "//", -1)[0]
			if reDefineMacro.MatchString(lastField) {
				line = COMMENT_C_LINE + line
			}

			fmt.Print(line)
			continue
		}
		if isDefine && line == "\n" {
			fmt.Print(")\n\n")
			isDefine = false
			continue
		}

		// === Translate structs.
		if !isStruct {
			if sub := reStruct.FindStringSubmatch(line); sub != nil {
				isStruct = true

				if isDefine {
					fmt.Print(")\n")
					isDefine = false
				}

				fmt.Printf("type %s struct {\n", strings.Title(sub[2]))
				continue
			}
		} else {
			if sub := reStructField.FindStringSubmatch(line); sub != nil {
				// Translate the field type.
				gotype, ok := ctypeTogo(sub[1], &extraType)

				// === Translate the field name.
				fieldName := reStructFieldName.FindStringSubmatch(sub[2])
				_fieldName := ""

				if fieldName[1] != "" {
					_fieldName = fieldName[2]
				} else {
					_fieldName = fieldName[0]
				}
				// ===

				line = fmt.Sprintf("%s %s %s",
					strings.Title(_fieldName), gotype, sub[3])

				// C type not found.
				if !ok {
					line = COMMENT_C_LINE + line
				}

				fmt.Print(line)
				continue
			}
			if strings.HasPrefix(line, "}") {
				fmt.Print(strings.Replace(line, ";", "", 1))
				isStruct = false
				continue
			}
		}

		// Comment another C lines.
		//if line != "\n" && !strings.HasPrefix(line, "//") {
		if !reSkip.MatchString(line) {
			line = COMMENT_C_LINE + line
		}

		fmt.Print(line)
	}

	return nil
}

// Translates a C type definition into Go definition. The C header could have
// defined new types so they're checked in the firs place.
func ctypeTogo(ctype string, extraCtype *vector.StringVector) (gotype string, ok bool) {
	for _, v := range *extraCtype {
		if v == ctype {
			return ctype, true
		}
	}

	switch ctype {
	case "char", "signed char", "signed short int", "short int", "short":
		return "int8", true
	case "unsigned char", "unsigned short int", "unsigned short":
		return "uint8", true
	case "int", "signed int":
		return "int16", true
	case "unsigned int", "unsigned":
		return "uint16", true
	case "signed long int", "long int", "long":
		return "int32", true
	case "unsigned long int", "unsigned long":
		return "uint32", true
	case "float":
		return "float32", true
	case "double", "long double":
		return "float64", true
	}
	return ctype, false
}

func main() {
	var (
		system      = flag.String("s", "", "The operating system")
		pkgName     = flag.String("p", "", "The name of the package")
		listSystems = flag.Bool("l", false, "List of valid systems")
	)

	validSystems := []string{"darwin", "freebsd", "linux", "windows"}
	var isSystem bool

	// === Parse the flags
	flag.Usage = usage
	flag.Parse()

	if *listSystems {
		fmt.Print("  = Systems\n\n  ")
		fmt.Println(validSystems)
		os.Exit(0)
	}
	if len(os.Args) == 1 || *system == "" || *pkgName == "" {
		usage()
	}

	*system = strings.ToLower(*system)

	for _, v := range validSystems {
		if v == *system {
			isSystem = true
			break
		}
	}
	if !isSystem {
		fmt.Fprintf(os.Stderr, "ERROR: System passed in flag 's' is invalid\n")
		os.Exit(1)
	}

	// === Update header
	cmd := strings.Join(os.Args, " ")
	goHeader = strings.Replace(goHeader, "{cmd}", path.Base(cmd), 1)
	goHeader = strings.Replace(goHeader, "{pkg}", *pkgName, 1)

	File := "../test/header.h" // !!!
	if err := translateC(File); err != nil {
		fmt.Fprintf(os.Stderr, err.String())
		os.Exit(1)
	}
}

<|endoftext|>


<commit_before>package google

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"runtime"
	"strings"

	"github.com/hashicorp/terraform/helper/logging"
	"github.com/hashicorp/terraform/helper/pathorcontents"
	"github.com/hashicorp/terraform/terraform"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
	"google.golang.org/api/bigquery/v2"
	"google.golang.org/api/cloudbilling/v1"
	"google.golang.org/api/cloudfunctions/v1"
	"google.golang.org/api/cloudiot/v1"
	"google.golang.org/api/cloudkms/v1"
	"google.golang.org/api/cloudresourcemanager/v1"
	resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
	computeBeta "google.golang.org/api/compute/v0.beta"
	"google.golang.org/api/compute/v1"
	"google.golang.org/api/container/v1"
	containerBeta "google.golang.org/api/container/v1beta1"
	"google.golang.org/api/dataflow/v1b3"
	"google.golang.org/api/dataproc/v1"
	"google.golang.org/api/dns/v1"
	"google.golang.org/api/iam/v1"
	cloudlogging "google.golang.org/api/logging/v2"
	"google.golang.org/api/pubsub/v1"
	"google.golang.org/api/runtimeconfig/v1beta1"
	"google.golang.org/api/servicemanagement/v1"
	"google.golang.org/api/sourcerepo/v1"
	"google.golang.org/api/spanner/v1"
	"google.golang.org/api/sqladmin/v1beta4"
	"google.golang.org/api/storage/v1"
)

// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
	Credentials string
	Project     string
	Region      string
	Zone        string

	client    *http.Client
	userAgent string

	tokenSource oauth2.TokenSource

	clientBilling                *cloudbilling.Service
	clientCompute                *compute.Service
	clientComputeBeta            *computeBeta.Service
	clientContainer              *container.Service
	clientContainerBeta          *containerBeta.Service
	clientDataproc               *dataproc.Service
	clientDataflow               *dataflow.Service
	clientDns                    *dns.Service
	clientKms                    *cloudkms.Service
	clientLogging                *cloudlogging.Service
	clientPubsub                 *pubsub.Service
	clientResourceManager        *cloudresourcemanager.Service
	clientResourceManagerV2Beta1 *resourceManagerV2Beta1.Service
	clientRuntimeconfig          *runtimeconfig.Service
	clientSpanner                *spanner.Service
	clientSourceRepo             *sourcerepo.Service
	clientStorage                *storage.Service
	clientSqlAdmin               *sqladmin.Service
	clientIAM                    *iam.Service
	clientServiceMan             *servicemanagement.APIService
	clientBigQuery               *bigquery.Service
	clientCloudFunctions         *cloudfunctions.Service
	clientCloudIoT               *cloudiot.Service

	bigtableClientFactory *BigtableClientFactory
}

func (c *Config) loadAndValidate() error {
	var account accountFile
	clientScopes := []string{
		"https://www.googleapis.com/auth/compute",
		"https://www.googleapis.com/auth/cloud-platform",
		"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
		"https://www.googleapis.com/auth/devstorage.full_control",
	}

	var client *http.Client
	var tokenSource oauth2.TokenSource

	if c.Credentials != "" {
		contents, _, err := pathorcontents.Read(c.Credentials)
		if err != nil {
			return fmt.Errorf("Error loading credentials: %s", err)
		}

		// Assume account_file is a JSON string
		if err := parseJSON(&account, contents); err != nil {
			return fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
		}

		// Get the token for use in our requests
		log.Printf("[INFO] Requesting Google token...")
		log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
		log.Printf("[INFO]   -- Scopes: %s", clientScopes)
		log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))

		conf := jwt.Config{
			Email:      account.ClientEmail,
			PrivateKey: []byte(account.PrivateKey),
			Scopes:     clientScopes,
			TokenURL:   "https://accounts.google.com/o/oauth2/token",
		}

		// Initiate an http.Client. The following GET request will be
		// authorized and authenticated on the behalf of
		// your service account.
		client = conf.Client(context.Background())

		tokenSource = conf.TokenSource(context.Background())
	} else {
		log.Printf("[INFO] Authenticating using DefaultClient")
		err := error(nil)
		client, err = google.DefaultClient(context.Background(), clientScopes...)
		if err != nil {
			return err
		}

		tokenSource, err = google.DefaultTokenSource(context.Background(), clientScopes...)
		if err != nil {
			return err
		}
	}

	c.tokenSource = tokenSource

	client.Transport = logging.NewTransport("Google", client.Transport)

	versionString := terraform.VersionString()
	userAgent := fmt.Sprintf(
		"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)

	c.client = client
	c.userAgent = userAgent

	var err error

	log.Printf("[INFO] Instantiating GCE client...")
	c.clientCompute, err = compute.New(client)
	if err != nil {
		return err
	}
	c.clientCompute.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GCE Beta client...")
	c.clientComputeBeta, err = computeBeta.New(client)
	if err != nil {
		return err
	}
	c.clientComputeBeta.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GKE client...")
	c.clientContainer, err = container.New(client)
	if err != nil {
		return err
	}
	c.clientContainer.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GKE Beta client...")
	c.clientContainerBeta, err = containerBeta.New(client)
	if err != nil {
		return err
	}
	c.clientContainerBeta.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud DNS client...")
	c.clientDns, err = dns.New(client)
	if err != nil {
		return err
	}
	c.clientDns.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud KMS Client...")
	c.clientKms, err = cloudkms.New(client)
	if err != nil {
		return err
	}
	c.clientKms.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Stackdriver Logging client...")
	c.clientLogging, err = cloudlogging.New(client)
	if err != nil {
		return err
	}
	c.clientLogging.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Storage Client...")
	c.clientStorage, err = storage.New(client)
	if err != nil {
		return err
	}
	c.clientStorage.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google SqlAdmin Client...")
	c.clientSqlAdmin, err = sqladmin.New(client)
	if err != nil {
		return err
	}
	c.clientSqlAdmin.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Pubsub Client...")
	c.clientPubsub, err = pubsub.New(client)
	if err != nil {
		return err
	}
	c.clientPubsub.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Dataflow Client...")
	c.clientDataflow, err = dataflow.New(client)
	if err != nil {
		return err
	}
	c.clientDataflow.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud ResourceManager Client...")
	c.clientResourceManager, err = cloudresourcemanager.New(client)
	if err != nil {
		return err
	}
	c.clientResourceManager.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud ResourceManager V Client...")
	c.clientResourceManagerV2Beta1, err = resourceManagerV2Beta1.New(client)
	if err != nil {
		return err
	}
	c.clientResourceManagerV2Beta1.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Runtimeconfig Client...")
	c.clientRuntimeconfig, err = runtimeconfig.New(client)
	if err != nil {
		return err
	}
	c.clientRuntimeconfig.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud IAM Client...")
	c.clientIAM, err = iam.New(client)
	if err != nil {
		return err
	}
	c.clientIAM.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Service Management Client...")
	c.clientServiceMan, err = servicemanagement.New(client)
	if err != nil {
		return err
	}
	c.clientServiceMan.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Billing Client...")
	c.clientBilling, err = cloudbilling.New(client)
	if err != nil {
		return err
	}
	c.clientBilling.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud BigQuery Client...")
	c.clientBigQuery, err = bigquery.New(client)
	if err != nil {
		return err
	}
	c.clientBigQuery.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud CloudFunctions Client...")
	c.clientCloudFunctions, err = cloudfunctions.New(client)
	if err != nil {
		return err
	}
	c.clientCloudFunctions.UserAgent = userAgent

	c.bigtableClientFactory = &BigtableClientFactory{
		UserAgent:   userAgent,
		TokenSource: tokenSource,
	}

	log.Printf("[INFO] Instantiating Google Cloud Source Repo Client...")
	c.clientSourceRepo, err = sourcerepo.New(client)
	if err != nil {
		return err
	}
	c.clientSourceRepo.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Spanner Client...")
	c.clientSpanner, err = spanner.New(client)
	if err != nil {
		return err
	}
	c.clientSpanner.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Dataproc Client...")
	c.clientDataproc, err = dataproc.New(client)
	if err != nil {
		return err
	}
	c.clientDataproc.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud IoT Core Client...")
	c.clientCloudIoT, err = cloudiot.New(client)
	if err != nil {
		return err
	}
	c.clientCloudIoT.UserAgent = userAgent

	return nil
}

// accountFile represents the structure of the account file JSON file.
type accountFile struct {
	PrivateKeyId string `json:"private_key_id"`
	PrivateKey   string `json:"private_key"`
	ClientEmail  string `json:"client_email"`
	ClientId     string `json:"client_id"`
}

func parseJSON(result interface{}, contents string) error {
	r := strings.NewReader(contents)
	dec := json.NewDecoder(r)

	return dec.Decode(result)
}
<commit_msg>Use standard user-agent header (#1332)<commit_after>package google

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/hashicorp/terraform/helper/logging"
	"github.com/hashicorp/terraform/helper/pathorcontents"
	"github.com/hashicorp/terraform/version"

	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"golang.org/x/oauth2/jwt"
	"google.golang.org/api/bigquery/v2"
	"google.golang.org/api/cloudbilling/v1"
	"google.golang.org/api/cloudfunctions/v1"
	"google.golang.org/api/cloudiot/v1"
	"google.golang.org/api/cloudkms/v1"
	"google.golang.org/api/cloudresourcemanager/v1"
	resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
	computeBeta "google.golang.org/api/compute/v0.beta"
	"google.golang.org/api/compute/v1"
	"google.golang.org/api/container/v1"
	containerBeta "google.golang.org/api/container/v1beta1"
	"google.golang.org/api/dataflow/v1b3"
	"google.golang.org/api/dataproc/v1"
	"google.golang.org/api/dns/v1"
	"google.golang.org/api/iam/v1"
	cloudlogging "google.golang.org/api/logging/v2"
	"google.golang.org/api/pubsub/v1"
	"google.golang.org/api/runtimeconfig/v1beta1"
	"google.golang.org/api/servicemanagement/v1"
	"google.golang.org/api/sourcerepo/v1"
	"google.golang.org/api/spanner/v1"
	"google.golang.org/api/sqladmin/v1beta4"
	"google.golang.org/api/storage/v1"
)

// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
	Credentials string
	Project     string
	Region      string
	Zone        string

	client    *http.Client
	userAgent string

	tokenSource oauth2.TokenSource

	clientBilling                *cloudbilling.Service
	clientCompute                *compute.Service
	clientComputeBeta            *computeBeta.Service
	clientContainer              *container.Service
	clientContainerBeta          *containerBeta.Service
	clientDataproc               *dataproc.Service
	clientDataflow               *dataflow.Service
	clientDns                    *dns.Service
	clientKms                    *cloudkms.Service
	clientLogging                *cloudlogging.Service
	clientPubsub                 *pubsub.Service
	clientResourceManager        *cloudresourcemanager.Service
	clientResourceManagerV2Beta1 *resourceManagerV2Beta1.Service
	clientRuntimeconfig          *runtimeconfig.Service
	clientSpanner                *spanner.Service
	clientSourceRepo             *sourcerepo.Service
	clientStorage                *storage.Service
	clientSqlAdmin               *sqladmin.Service
	clientIAM                    *iam.Service
	clientServiceMan             *servicemanagement.APIService
	clientBigQuery               *bigquery.Service
	clientCloudFunctions         *cloudfunctions.Service
	clientCloudIoT               *cloudiot.Service

	bigtableClientFactory *BigtableClientFactory
}

func (c *Config) loadAndValidate() error {
	var account accountFile
	clientScopes := []string{
		"https://www.googleapis.com/auth/compute",
		"https://www.googleapis.com/auth/cloud-platform",
		"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
		"https://www.googleapis.com/auth/devstorage.full_control",
	}

	var client *http.Client
	var tokenSource oauth2.TokenSource

	if c.Credentials != "" {
		contents, _, err := pathorcontents.Read(c.Credentials)
		if err != nil {
			return fmt.Errorf("Error loading credentials: %s", err)
		}

		// Assume account_file is a JSON string
		if err := parseJSON(&account, contents); err != nil {
			return fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
		}

		// Get the token for use in our requests
		log.Printf("[INFO] Requesting Google token...")
		log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
		log.Printf("[INFO]   -- Scopes: %s", clientScopes)
		log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))

		conf := jwt.Config{
			Email:      account.ClientEmail,
			PrivateKey: []byte(account.PrivateKey),
			Scopes:     clientScopes,
			TokenURL:   "https://accounts.google.com/o/oauth2/token",
		}

		// Initiate an http.Client. The following GET request will be
		// authorized and authenticated on the behalf of
		// your service account.
		client = conf.Client(context.Background())

		tokenSource = conf.TokenSource(context.Background())
	} else {
		log.Printf("[INFO] Authenticating using DefaultClient")
		err := error(nil)
		client, err = google.DefaultClient(context.Background(), clientScopes...)
		if err != nil {
			return err
		}

		tokenSource, err = google.DefaultTokenSource(context.Background(), clientScopes...)
		if err != nil {
			return err
		}
	}

	c.tokenSource = tokenSource

	client.Transport = logging.NewTransport("Google", client.Transport)

	projectURL := "https://www.terraform.io"
	userAgent := fmt.Sprintf("Terraform/%s (+%s)",
		version.String(), projectURL)

	c.client = client
	c.userAgent = userAgent

	var err error

	log.Printf("[INFO] Instantiating GCE client...")
	c.clientCompute, err = compute.New(client)
	if err != nil {
		return err
	}
	c.clientCompute.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GCE Beta client...")
	c.clientComputeBeta, err = computeBeta.New(client)
	if err != nil {
		return err
	}
	c.clientComputeBeta.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GKE client...")
	c.clientContainer, err = container.New(client)
	if err != nil {
		return err
	}
	c.clientContainer.UserAgent = userAgent

	log.Printf("[INFO] Instantiating GKE Beta client...")
	c.clientContainerBeta, err = containerBeta.New(client)
	if err != nil {
		return err
	}
	c.clientContainerBeta.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud DNS client...")
	c.clientDns, err = dns.New(client)
	if err != nil {
		return err
	}
	c.clientDns.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud KMS Client...")
	c.clientKms, err = cloudkms.New(client)
	if err != nil {
		return err
	}
	c.clientKms.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Stackdriver Logging client...")
	c.clientLogging, err = cloudlogging.New(client)
	if err != nil {
		return err
	}
	c.clientLogging.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Storage Client...")
	c.clientStorage, err = storage.New(client)
	if err != nil {
		return err
	}
	c.clientStorage.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google SqlAdmin Client...")
	c.clientSqlAdmin, err = sqladmin.New(client)
	if err != nil {
		return err
	}
	c.clientSqlAdmin.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Pubsub Client...")
	c.clientPubsub, err = pubsub.New(client)
	if err != nil {
		return err
	}
	c.clientPubsub.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Dataflow Client...")
	c.clientDataflow, err = dataflow.New(client)
	if err != nil {
		return err
	}
	c.clientDataflow.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud ResourceManager Client...")
	c.clientResourceManager, err = cloudresourcemanager.New(client)
	if err != nil {
		return err
	}
	c.clientResourceManager.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud ResourceManager V Client...")
	c.clientResourceManagerV2Beta1, err = resourceManagerV2Beta1.New(client)
	if err != nil {
		return err
	}
	c.clientResourceManagerV2Beta1.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Runtimeconfig Client...")
	c.clientRuntimeconfig, err = runtimeconfig.New(client)
	if err != nil {
		return err
	}
	c.clientRuntimeconfig.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud IAM Client...")
	c.clientIAM, err = iam.New(client)
	if err != nil {
		return err
	}
	c.clientIAM.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Service Management Client...")
	c.clientServiceMan, err = servicemanagement.New(client)
	if err != nil {
		return err
	}
	c.clientServiceMan.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Billing Client...")
	c.clientBilling, err = cloudbilling.New(client)
	if err != nil {
		return err
	}
	c.clientBilling.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud BigQuery Client...")
	c.clientBigQuery, err = bigquery.New(client)
	if err != nil {
		return err
	}
	c.clientBigQuery.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud CloudFunctions Client...")
	c.clientCloudFunctions, err = cloudfunctions.New(client)
	if err != nil {
		return err
	}
	c.clientCloudFunctions.UserAgent = userAgent

	c.bigtableClientFactory = &BigtableClientFactory{
		UserAgent:   userAgent,
		TokenSource: tokenSource,
	}

	log.Printf("[INFO] Instantiating Google Cloud Source Repo Client...")
	c.clientSourceRepo, err = sourcerepo.New(client)
	if err != nil {
		return err
	}
	c.clientSourceRepo.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Spanner Client...")
	c.clientSpanner, err = spanner.New(client)
	if err != nil {
		return err
	}
	c.clientSpanner.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud Dataproc Client...")
	c.clientDataproc, err = dataproc.New(client)
	if err != nil {
		return err
	}
	c.clientDataproc.UserAgent = userAgent

	log.Printf("[INFO] Instantiating Google Cloud IoT Core Client...")
	c.clientCloudIoT, err = cloudiot.New(client)
	if err != nil {
		return err
	}
	c.clientCloudIoT.UserAgent = userAgent

	return nil
}

// accountFile represents the structure of the account file JSON file.
type accountFile struct {
	PrivateKeyId string `json:"private_key_id"`
	PrivateKey   string `json:"private_key"`
	ClientEmail  string `json:"client_email"`
	ClientId     string `json:"client_id"`
}

func parseJSON(result interface{}, contents string) error {
	r := strings.NewReader(contents)
	dec := json.NewDecoder(r)

	return dec.Decode(result)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/base32"
	"encoding/base64"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"sort"
	"strings"

	"github.com/cmars/basen"
)

var decoder = flag.String("decode", "none", "input decoding method")
var encoder = flag.String("encode", "none", "output encoding method")
var help = flag.Bool("help", false, "show help")

type codec struct {
	label          string
	raw            bool
	EncodeToString func([]byte) string
	DecodeString   func(string) ([]byte, error)
}

var codecs = map[string]codec{
	"none": codec{
		label:          "No encoding, raw binary",
		raw:            true,
		EncodeToString: func(b []byte) string { return string(b) },
		DecodeString:   func(s string) ([]byte, error) { return []byte(s), nil },
	},
	"16": codec{
		EncodeToString: hex.EncodeToString,
		DecodeString:   hex.DecodeString,
	},
	"32": codec{
		EncodeToString: base32.StdEncoding.EncodeToString,
		DecodeString:   base32.StdEncoding.DecodeString,
	},
	"32hex": codec{
		label:          "Base 32 \"Extended Hex Alphabet\"",
		EncodeToString: base32.HexEncoding.EncodeToString,
		DecodeString:   base32.HexEncoding.DecodeString,
	},
	"58": codec{
		EncodeToString: basen.Base58.EncodeToString,
		DecodeString:   basen.Base58.DecodeString,
	},
	"62": codec{
		EncodeToString: basen.Base62.EncodeToString,
		DecodeString:   basen.Base62.DecodeString,
	},
	"64": codec{
		EncodeToString: base64.StdEncoding.EncodeToString,
		DecodeString:   base64.StdEncoding.DecodeString,
	},
	"64url": codec{
		label:          "Base 64 alternate URL encoding",
		EncodeToString: base64.URLEncoding.EncodeToString,
		DecodeString:   base64.URLEncoding.DecodeString,
	},
}

func init() {
	var ok bool
	codecs["raw"], ok = codecs["none"]
	if !ok {
		panic("missing codec 'none'")
	}
	codecs["hex"], ok = codecs["16"]
	if !ok {
		panic("missing codec '16'")
	}
}

func die(err error) {
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	os.Exit(0)
}

func showCodecs() {
	var lines []string
	log.Printf("supported codecs:")
	log.Printf("% 10s  %s", "codec", "description")
	log.Printf("----------- -------------------------------------------------------------")
	for k, v := range codecs {
		if v.label == "" {
			lines = append(lines, fmt.Sprintf("% 10s: base %s", k, k))
		} else {
			lines = append(lines, fmt.Sprintf("% 10s: %s", k, v.label))
		}
	}
	sort.Strings(lines)
	for _, line := range lines {
		log.Println(line)
	}
}

func main() {
	flag.Parse()

	if *help {
		showCodecs()
		flag.PrintDefaults()
		die(nil)
	}

	var input io.Reader
	if flag.NArg() > 0 {
		f, err := os.Open(flag.Arg(0))
		if err != nil {
			die(err)
		}
		defer f.Close()
		input = f
	} else {
		input = os.Stdin
	}

	inCodec, ok := codecs[*decoder]
	if !ok {
		showCodecs()
		die(fmt.Errorf("unsupported codec: %q", *decoder))
	}
	outCodec, ok := codecs[*encoder]
	if !ok {
		showCodecs()
		die(fmt.Errorf("unsupported codec: %q", *encoder))
	}

	inputBuf, err := ioutil.ReadAll(input)
	if err != nil {
		die(err)
	}
	inputStr := string(inputBuf)
	if !inCodec.raw {
		inputStr = strings.TrimSpace(inputStr)
	}

	contents, err := inCodec.DecodeString(inputStr)
	if err != nil {
		die(err)
	}

	outputStr := outCodec.EncodeToString(contents)
	fmt.Print(outputStr)
	if !outCodec.raw {
		fmt.Print("\n")
	}
	die(nil)
}
<commit_msg>Prettify basen command errors<commit_after>package main

import (
	"encoding/base32"
	"encoding/base64"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"sort"
	"strings"

	"github.com/cmars/basen"
)

var decoder = flag.String("decode", "none", "input decoding method")
var encoder = flag.String("encode", "none", "output encoding method")
var help = flag.Bool("help", false, "show help")

type codec struct {
	label          string
	raw            bool
	EncodeToString func([]byte) string
	DecodeString   func(string) ([]byte, error)
}

var codecs = map[string]codec{
	"none": codec{
		label:          "No encoding, raw binary",
		raw:            true,
		EncodeToString: func(b []byte) string { return string(b) },
		DecodeString:   func(s string) ([]byte, error) { return []byte(s), nil },
	},
	"16": codec{
		EncodeToString: hex.EncodeToString,
		DecodeString:   hex.DecodeString,
	},
	"32": codec{
		EncodeToString: base32.StdEncoding.EncodeToString,
		DecodeString:   base32.StdEncoding.DecodeString,
	},
	"32hex": codec{
		label:          "Base 32 \"Extended Hex Alphabet\"",
		EncodeToString: base32.HexEncoding.EncodeToString,
		DecodeString:   base32.HexEncoding.DecodeString,
	},
	"58": codec{
		EncodeToString: basen.Base58.EncodeToString,
		DecodeString:   basen.Base58.DecodeString,
	},
	"62": codec{
		EncodeToString: basen.Base62.EncodeToString,
		DecodeString:   basen.Base62.DecodeString,
	},
	"64": codec{
		EncodeToString: base64.StdEncoding.EncodeToString,
		DecodeString:   base64.StdEncoding.DecodeString,
	},
	"64url": codec{
		label:          "Base 64 alternate URL encoding",
		EncodeToString: base64.URLEncoding.EncodeToString,
		DecodeString:   base64.URLEncoding.DecodeString,
	},
}

func init() {
	var ok bool
	codecs["raw"], ok = codecs["none"]
	if !ok {
		panic("missing codec 'none'")
	}
	codecs["hex"], ok = codecs["16"]
	if !ok {
		panic("missing codec '16'")
	}
}

func die(err error) {
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	os.Exit(0)
}

func showCodecs() {
	var lines []string
	log.Printf("supported codecs:")
	log.Printf("% 10s  %s", "codec", "description")
	log.Printf("----------- -------------------------------------------------------------")
	for k, v := range codecs {
		if v.label == "" {
			lines = append(lines, fmt.Sprintf("% 10s: base %s", k, k))
		} else {
			lines = append(lines, fmt.Sprintf("% 10s: %s", k, v.label))
		}
	}
	sort.Strings(lines)
	for _, line := range lines {
		log.Println(line)
	}
}

func main() {
	log.SetFlags(0)
	log.SetPrefix("basen: ")
	flag.Parse()

	if *help {
		showCodecs()
		flag.PrintDefaults()
		die(nil)
	}

	var input io.Reader
	if flag.NArg() > 0 {
		f, err := os.Open(flag.Arg(0))
		if err != nil {
			die(err)
		}
		defer f.Close()
		input = f
	} else {
		input = os.Stdin
	}

	inCodec, ok := codecs[*decoder]
	if !ok {
		showCodecs()
		die(fmt.Errorf("unsupported codec: %q", *decoder))
	}
	outCodec, ok := codecs[*encoder]
	if !ok {
		showCodecs()
		die(fmt.Errorf("unsupported codec: %q", *encoder))
	}

	inputBuf, err := ioutil.ReadAll(input)
	if err != nil {
		die(err)
	}
	inputStr := string(inputBuf)
	if !inCodec.raw {
		inputStr = strings.TrimSpace(inputStr)
	}

	contents, err := inCodec.DecodeString(inputStr)
	if err != nil {
		die(err)
	}

	outputStr := outCodec.EncodeToString(contents)
	fmt.Print(outputStr)
	if !outCodec.raw {
		fmt.Print("\n")
	}
	die(nil)
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"flag"
	"fmt"
	"os"

	"github.com/DaemonNews/dnews/src"
)

func insertMD(db *sql.DB, a *dnews.Article) (int, error) {
	var id int
	err := db.QueryRow(`INSERT INTO articles (title, body) values ($1, $2) returning id`, a.Title, a.Body).Scan(&id)
	return id, err
}

func main() {
	var mdFile = flag.String("mdfile", "", "Path to markdown file to import.")
	var pub = flag.String("pubkey", "", "Path to public key for signature verification.")
	var sig = flag.String("sig", "", "Path to signature of article.")
	//var htmlOut = flag.Bool("html", false, "Output in HTML")
	var add = flag.Bool("a", false, "Add aticle to DB")
	var live = flag.Bool("l", false, "Set article to be live")
	flag.Parse()

	if *mdFile == "" {
		fmt.Println("please specify file with -mdfile")
		os.Exit(1)
	}

	var a = dnews.Article{}
	a.LoadFromFile(*mdFile)

	if *pub == "" || *sig == "" {
		fmt.Println("Please specify -pubkey and -sig!")
		os.Exit(1)
	}

	p := dnews.LoadFileOrDie(*pub)
	a.Signature = dnews.LoadFileOrDie(*sig)

	ok, err := a.Verify(p)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if !*ok {
		fmt.Println("Signature NOT ok!")
		os.Exit(1)
	}

	fmt.Println("Signature OK")
	a.Signed = *ok
	a.Live = *live

	if *add {
		id, err := dnews.InsertArticle(a)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Printf("Added article! (%d)\n", *id)
	}
}
<commit_msg>remove unused func, fix db<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/DaemonNews/dnews/src"
)

func main() {
	var mdFile = flag.String("mdfile", "", "Path to markdown file to import.")
	var pub = flag.String("pubkey", "", "Path to public key for signature verification.")
	var sig = flag.String("sig", "", "Path to signature of article.")
	//var htmlOut = flag.Bool("html", false, "Output in HTML")
	var add = flag.Bool("a", false, "Add aticle to DB")
	var live = flag.Bool("l", false, "Set article to be live")
	flag.Parse()

	db, err := dnews.DBConnect()

	if *mdFile == "" {
		fmt.Println("please specify file with -mdfile")
		os.Exit(1)
	}

	var a = dnews.Article{}
	a.LoadFromFile(*mdFile)

	if *pub == "" || *sig == "" {
		fmt.Println("Please specify -pubkey and -sig!")
		os.Exit(1)
	}

	p := dnews.LoadFileOrDie(*pub)
	a.Signature = dnews.LoadFileOrDie(*sig)

	ok, err := a.Verify(p)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if !*ok {
		fmt.Println("Signature NOT ok!")
		os.Exit(1)
	}

	fmt.Println("Signature OK")
	a.Signed = *ok
	a.Live = *live

	if *add {
		id, err := dnews.InsertArticle(db, a)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Printf("Added article! (%d)\n", *id)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	"github.com/go-kit/kit/log"
	"github.com/spf13/pflag"
	"golang.org/x/net/context"
	"k8s.io/kubernetes/pkg/client/restclient"

	"github.com/weaveworks/fluxy"
	"github.com/weaveworks/fluxy/platform/kubernetes"
	"github.com/weaveworks/fluxy/registry"
)

func main() {
	// Flag domain.
	fs := pflag.NewFlagSet("default", pflag.ExitOnError)
	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "DESCRIPTION\n")
		fmt.Fprintf(os.Stderr, "  fluxd is a deployment daemon.\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "FLAGS\n")
		fs.PrintDefaults()
	}
	// This mirrors how kubectl extracts information from the environment.
	var (
		listenAddr                = fs.StringP("listen", "l", ":3030", "Listen address for Flux API clients")
		registryCredentials       = fs.StringP("registry-credentials", "", "", "Path to image registry credentials file, in the format of ~/.docker/config.json")
		kubernetesEnabled         = fs.BoolP("kubernetes", "", false, "Enable Kubernetes platform")
		kubernetesKubectl         = fs.StringP("kubernetes-kubectl", "", "", "Optional, explicit path to kubectl tool")
		kubernetesHost            = fs.StringP("kubernetes-host", "", "", "Kubernetes host, e.g. http://10.11.12.13:8080")
		kubernetesUsername        = fs.StringP("kubernetes-username", "", "", "Kubernetes HTTP basic auth username")
		kubernetesPassword        = fs.StringP("kubernetes-password", "", "", "Kubernetes HTTP basic auth password")
		kubernetesClientCert      = fs.StringP("kubernetes-client-certificate", "", "", "Path to Kubernetes client certification file for TLS")
		kubernetesClientKey       = fs.StringP("kubernetes-client-key", "", "", "Path to Kubernetes client key file for TLS")
		kubernetesCertAuthority   = fs.StringP("kubernetes-certificate-authority", "", "", "Path to Kubernetes cert file for certificate authority")
		kubernetesBearerTokenFile = fs.StringP("kubernetes-bearer-token-file", "", "", "Path to file containing Kubernetes Bearer Token file")
	)
	fs.Parse(os.Args)

	// Logger domain.
	var logger log.Logger
	{
		logger = log.NewLogfmtLogger(os.Stderr)
		logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
		logger = log.NewContext(logger).With("caller", log.DefaultCaller)
	}

	// Debug.
	fs.Visit(func(f *pflag.Flag) {
		logger.Log(f.Name, f.Value)
	})

	// Registry component.
	var reg *registry.Client
	{
		logger := log.NewContext(logger).With("component", "registry")
		creds := registry.NoCredentials()
		if *registryCredentials != "" {
			logger.Log("credentials", *registryCredentials)
			c, err := registry.CredentialsFromFile(*registryCredentials)
			if err != nil {
				logger.Log("err", err)
				os.Exit(1)
			}
			creds = c
		} else {
			logger.Log("credentials", "none")
		}
		reg = &registry.Client{
			Credentials: creds,
			Logger:      logger,
		}
	}

	// Platform component.
	var k8s *kubernetes.Cluster
	{
		logger := log.NewContext(logger).With("component", "platform")
		if *kubernetesEnabled {
			logger.Log("kind", "Kubernetes", "host", kubernetesHost)

			var bearerToken string
			if *kubernetesBearerTokenFile != "" {
				buf, err := ioutil.ReadFile(*kubernetesBearerTokenFile)
				if err != nil {
					logger.Log("err", err)
					os.Exit(1)
				}
				bearerToken = string(buf)
			}

			var err error
			k8s, err = kubernetes.NewCluster(&restclient.Config{
				Host:        *kubernetesHost,
				Username:    *kubernetesUsername,
				Password:    *kubernetesPassword,
				BearerToken: bearerToken,
				TLSClientConfig: restclient.TLSClientConfig{
					CertFile: *kubernetesClientCert,
					KeyFile:  *kubernetesClientKey,
					CAFile:   *kubernetesCertAuthority,
				},
			}, *kubernetesKubectl, logger)
			if err != nil {
				logger.Log("err", err)
				os.Exit(1)
			}

			if services, err := k8s.Services("default"); err != nil {
				logger.Log("services", err)
			} else {
				logger.Log("services", len(services))
			}
		} else {
			logger.Log("kind", "none")
		}
	}

	// Service (business logic) domain.
	var service flux.Service
	{
		service = flux.NewService(reg, k8s)
		service = flux.LoggingMiddleware(logger)(service)
	}

	// Endpoint domain.
	var endpoints flux.Endpoints
	{
		endpoints = flux.MakeServerEndpoints(service)
	}

	// Mechanical stuff.
	errc := make(chan error)
	go func() {
		c := make(chan os.Signal)
		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
		errc <- fmt.Errorf("%s", <-c)
	}()

	// Transport domain.
	ctx := context.Background()
	go func() {
		logger := log.NewContext(logger).With("transport", "HTTP")
		logger.Log("addr", *listenAddr)
		h := flux.MakeHTTPHandler(ctx, endpoints, logger)
		errc <- http.ListenAndServe(*listenAddr, h)
	}()

	// Go!
	logger.Log("exit", <-errc)
}
<commit_msg>Feedback<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/signal"
	"syscall"

	"github.com/go-kit/kit/log"
	"github.com/spf13/pflag"
	"golang.org/x/net/context"
	"k8s.io/kubernetes/pkg/client/restclient"

	"github.com/weaveworks/fluxy"
	"github.com/weaveworks/fluxy/platform/kubernetes"
	"github.com/weaveworks/fluxy/registry"
)

func main() {
	// Flag domain.
	fs := pflag.NewFlagSet("default", pflag.ExitOnError)
	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "DESCRIPTION\n")
		fmt.Fprintf(os.Stderr, "  fluxd is a deployment daemon.\n")
		fmt.Fprintf(os.Stderr, "\n")
		fmt.Fprintf(os.Stderr, "FLAGS\n")
		fs.PrintDefaults()
	}
	// This mirrors how kubectl extracts information from the environment.
	var (
		listenAddr                = fs.StringP("listen", "l", ":3030", "Listen address for Flux API clients")
		registryCredentials       = fs.String("registry-credentials", "", "Path to image registry credentials file, in the format of ~/.docker/config.json")
		kubernetesEnabled         = fs.Bool("kubernetes", false, "Enable Kubernetes platform")
		kubernetesKubectl         = fs.String("kubernetes-kubectl", "", "Optional, explicit path to kubectl tool")
		kubernetesHost            = fs.String("kubernetes-host", "", "Kubernetes host, e.g. http://10.11.12.13:8080")
		kubernetesUsername        = fs.String("kubernetes-username", "", "Kubernetes HTTP basic auth username")
		kubernetesPassword        = fs.String("kubernetes-password", "", "Kubernetes HTTP basic auth password")
		kubernetesClientCert      = fs.String("kubernetes-client-certificate", "", "Path to Kubernetes client certification file for TLS")
		kubernetesClientKey       = fs.String("kubernetes-client-key", "", "Path to Kubernetes client key file for TLS")
		kubernetesCertAuthority   = fs.String("kubernetes-certificate-authority", "", "Path to Kubernetes cert file for certificate authority")
		kubernetesBearerTokenFile = fs.String("kubernetes-bearer-token-file", "", "Path to file containing Kubernetes Bearer Token file")
	)
	fs.Parse(os.Args)

	// Logger domain.
	var logger log.Logger
	{
		logger = log.NewLogfmtLogger(os.Stderr)
		logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
		logger = log.NewContext(logger).With("caller", log.DefaultCaller)
	}

	// Registry component.
	var reg *registry.Client
	{
		logger := log.NewContext(logger).With("component", "registry")
		creds := registry.NoCredentials()
		if *registryCredentials != "" {
			logger.Log("credentials", *registryCredentials)
			c, err := registry.CredentialsFromFile(*registryCredentials)
			if err != nil {
				logger.Log("err", err)
				os.Exit(1)
			}
			creds = c
		} else {
			logger.Log("credentials", "none")
		}
		reg = &registry.Client{
			Credentials: creds,
			Logger:      logger,
		}
	}

	// Platform component.
	var k8s *kubernetes.Cluster
	{
		logger := log.NewContext(logger).With("component", "platform")
		if *kubernetesEnabled {
			logger.Log("kind", "Kubernetes", "host", kubernetesHost)

			var bearerToken string
			if *kubernetesBearerTokenFile != "" {
				buf, err := ioutil.ReadFile(*kubernetesBearerTokenFile)
				if err != nil {
					logger.Log("err", err)
					os.Exit(1)
				}
				bearerToken = string(buf)
			}

			var err error
			k8s, err = kubernetes.NewCluster(&restclient.Config{
				Host:        *kubernetesHost,
				Username:    *kubernetesUsername,
				Password:    *kubernetesPassword,
				BearerToken: bearerToken,
				TLSClientConfig: restclient.TLSClientConfig{
					CertFile: *kubernetesClientCert,
					KeyFile:  *kubernetesClientKey,
					CAFile:   *kubernetesCertAuthority,
				},
			}, *kubernetesKubectl, logger)
			if err != nil {
				logger.Log("err", err)
				os.Exit(1)
			}

			if services, err := k8s.Services("default"); err != nil {
				logger.Log("services", err)
			} else {
				logger.Log("services", len(services))
			}
		} else {
			logger.Log("kind", "none")
		}
	}

	// Service (business logic) domain.
	var service flux.Service
	{
		service = flux.NewService(reg, k8s)
		service = flux.LoggingMiddleware(logger)(service)
	}

	// Endpoint domain.
	var endpoints flux.Endpoints
	{
		endpoints = flux.MakeServerEndpoints(service)
	}

	// Mechanical stuff.
	errc := make(chan error)
	go func() {
		c := make(chan os.Signal)
		signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
		errc <- fmt.Errorf("%s", <-c)
	}()

	// Transport domain.
	ctx := context.Background()
	go func() {
		logger := log.NewContext(logger).With("transport", "HTTP")
		logger.Log("addr", *listenAddr)
		h := flux.MakeHTTPHandler(ctx, endpoints, logger)
		errc <- http.ListenAndServe(*listenAddr, h)
	}()

	// Go!
	logger.Log("exit", <-errc)
}
<|endoftext|>


<commit_before>package main

import (
	"path/filepath"
	"os"
	"common"
)

func init() {
	// "gobox" has to be added here for two reasons
	// 1.) So it can't be removed, it's core functionalty
	// 2.) It causes cyclic dependencies
	Applets["gobox"] = Gobox
}

func run() {
	callname := filepath.Base(os.Args[0])
	applet, ok := Applets[callname]
	if !ok {
		panic(os.NewError("Could not find applet \"" + callname + "\""))
	}
	e := applet(os.Args)
	if e != nil {
		panic(e)
	}
}

func main() {
	defer func() {
		if p := recover(); p != nil {
			e, ok := p.(os.Error)
			if !ok {
				e = os.NewError("Some error occured")
			}
			common.DumpError(e)
		}
	}()
	run()
}
<commit_msg>Gobox: Added direct call<commit_after>package main

import (
	"path/filepath"
	"os"
	"common"
)

func init() {
	// "gobox" has to be added here for two reasons
	// 1.) So it can't be removed, it's core functionalty
	// 2.) It causes cyclic dependencies otherwise
	Applets["gobox"] = Gobox
}

func run() {
	callname := filepath.Base(os.Args[0])
	applet, ok := Applets[callname]
	if !ok {
		panic(os.NewError("Could not find applet \"" + callname + "\""))
	}

	// If the Gobox applet is called (i.e. the executable itself)
	// check, if the second parameter is an applet name.
	// If so, call that applet instead
	args := os.Args
	if applet == Gobox && len(args) >= 2 {
		subapplet, ok := Applets[args[1]]
		if ok {
			applet = subapplet
			args = args[1:]
		}
	}

	e := applet(args)
	if e != nil {
		panic(e)
	}
}

func main() {
	defer func() {
		if p := recover(); p != nil {
			e, ok := p.(os.Error)
			if !ok {
				e = os.NewError("Some error occured")
			}
			common.DumpError(e)
		}
	}()
	run()
}
<|endoftext|>