<commit_before>package helpers

import (
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	"github.com/pivotal-cf-experimental/cf-test-helpers/cf"
)

type ServiceInfo struct {
	ServiceName string
	ServiceProvider string
	PlanName string
	ServiceAuthToken string
}

type SuiteContext interface {
	Setup()
	Teardown()

	AdminUserContext() cf.UserContext
	RegularUserContext() cf.UserContext
}

type Environment struct {
	Context SuiteContext
	originalCfHomeDir string
	currentCfHomeDir string
	ServiceInfo ServiceInfo
	Config IntegrationConfig
}

func NewEnvironment(context SuiteContext, serviceInfo ServiceInfo, config IntegrationConfig) *Environment {
	return &Environment{Context: context, ServiceInfo: serviceInfo, Config: config}
}

func (e *Environment) Setup() {
	e.Context.Setup()

	cf.AsUser(e.Context.AdminUserContext(), func() {
		setUpSpaceWithUserAccess(e.Context.RegularUserContext())
		Eventually(cf.Cf("create-service-auth-token", e.ServiceInfo.ServiceName, e.ServiceInfo.ServiceProvider, e.ServiceInfo.ServiceAuthToken), 60).Should(Exit(0))
	})

	e.originalCfHomeDir, e.currentCfHomeDir = cf.InitiateUserContext(e.Context.RegularUserContext())
	cf.TargetSpace(e.Context.RegularUserContext())
}

func (e *Environment) Teardown() {
	e.Context.Teardown()

	cf.AsUser(e.Context.AdminUserContext(), func() {
		Eventually(cf.Cf("delete-service-auth-token", e.ServiceInfo.ServiceName, e.ServiceInfo.ServiceProvider, "-f"), 60).Should(Exit(0))
	})

	cf.RestoreUserContext(e.Context.RegularUserContext(), e.originalCfHomeDir, e.currentCfHomeDir)
}

func setUpSpaceWithUserAccess(uc cf.UserContext) {
	spaceSetupTimeout := 10
	Eventually(cf.Cf("create-space", "-o", uc.Org, uc.Space), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceManager"), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceDeveloper"), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceAuditor"), spaceSetupTimeout).Should(Exit(0))
}
<commit_msg>auth token creation is more resilient.<commit_after>package helpers

import (
	"time"
	"fmt"

	"github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	"github.com/pivotal-cf-experimental/cf-test-helpers/cf"
)

type ServiceInfo struct {
	ServiceName string
	ServiceProvider string
	PlanName string
	ServiceAuthToken string
}

type SuiteContext interface {
	Setup()
	Teardown()

	AdminUserContext() cf.UserContext
	RegularUserContext() cf.UserContext
}

type Environment struct {
	Context SuiteContext
	originalCfHomeDir string
	currentCfHomeDir string
	ServiceInfo ServiceInfo
	Config IntegrationConfig
}

func NewEnvironment(context SuiteContext, serviceInfo ServiceInfo, config IntegrationConfig) *Environment {
	return &Environment{Context: context, ServiceInfo: serviceInfo, Config: config}
}

func (e *Environment) Setup() {
	e.Context.Setup()

	cf.AsUser(e.Context.AdminUserContext(), func() {
		setUpSpaceWithUserAccess(e.Context.RegularUserContext())
		createAuthToken(e.ServiceInfo)
	})

	e.originalCfHomeDir, e.currentCfHomeDir = cf.InitiateUserContext(e.Context.RegularUserContext())
	cf.TargetSpace(e.Context.RegularUserContext())
}

func (e *Environment) Teardown() {
	cf.RestoreUserContext(e.Context.RegularUserContext(), e.originalCfHomeDir, e.currentCfHomeDir)
	e.Context.Teardown()
}

func setUpSpaceWithUserAccess(uc cf.UserContext) {
	spaceSetupTimeout := 10
	Eventually(cf.Cf("create-space", "-o", uc.Org, uc.Space), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceManager"), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceDeveloper"), spaceSetupTimeout).Should(Exit(0))
	Eventually(cf.Cf("set-space-role", uc.Username, uc.Org, uc.Space, "SpaceAuditor"), spaceSetupTimeout).Should(Exit(0))
}

func createAuthToken(serviceInfo ServiceInfo) {
	createAuthTokenSession := cf.Cf("create-service-auth-token", serviceInfo.ServiceName, serviceInfo.ServiceProvider, serviceInfo.ServiceAuthToken)

	select {
	case <-createAuthTokenSession.Out.Detect("OK"):
	case <-createAuthTokenSession.Out.Detect("The service auth token label is taken"):
		fmt.Println("It is ok that the create-service-auth-token command failed.  This just means the token is already in place.")
	case <-time.After(60 * time.Second):
		ginkgo.Fail("Failed to create auth token")
	}
	createAuthTokenSession.Out.CancelDetects()
}
<|endoftext|>


<commit_before>// Copyright (c) 2013-2014 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

// Returns a bitmask of possible Bishop moves from the given square.
func (p *Position) bishopMoves(square int) Bitmask {
	return p.bishopMovesAt(square, p.board)
}

// Returns a bitmask of possible Rook moves from the given square.
func (p *Position) rookMoves(square int) Bitmask {
	return p.rookMovesAt(square, p.board)
}

// Returns a bitmask of possible Bishop moves from the given square wherees
// other pieces on the board are represented by the explicit parameter.
func (p *Position) bishopMovesAt(square int, board Bitmask) Bitmask {
	magic := ((bishopMagic[square].mask & board) * bishopMagic[square].magic) >> 55
	return bishopMagicMoves[square][magic]
}

// Returns a bitmask of possible Rook moves from the given square wherees other
// pieces on the board are represented by the explicit parameter.
func (p *Position) rookMovesAt(square int, board Bitmask) Bitmask {
	magic := ((rookMagic[square].mask & board) * rookMagic[square].magic) >> 52
	return rookMagicMoves[square][magic]
}

func (p *Position) targets(square int) Bitmask {
	return p.targetsFor(square, p.pieces[square])
}

func (p *Position) targetsFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Pawn:
		bitmask = pawnMoves[color][square] & p.outposts[color^1]
		//
		// If the square in front of the pawn is empty then add it as possible
		// target.
		//
		if target := square + eight[color]; p.board.isClear(target) {
			bitmask.set(target)
			//
			// If the pawn is in its initial position and two squares in front of
			// the pawn are empty then add the second square as possible target.
			//
			if RelRow(square, color) == 1 {
				if target += eight[color]; p.board.isClear(target) {
					bitmask.set(target)
				}
			}
		}
		//
		// If the last move set the en-passant square and it is diagonally adjacent
		// to the current pawn, then add en-passant to the pawn's attack targets.
		//
		if p.enpassant != 0 && maskPawn[color][p.enpassant].isSet(square) {
			bitmask.set(p.enpassant)
		}
	case Knight:
		bitmask = knightMoves[square] & ^p.outposts[color]
	case Bishop:
		bitmask = p.bishopMoves(square) & ^p.outposts[color]
	case Rook:
		bitmask = p.rookMoves(square) & ^p.outposts[color]
	case Queen:
		bitmask = (p.bishopMoves(square) | p.rookMoves(square)) & ^p.outposts[color]
	case King:
		bitmask = kingMoves[square] & ^p.outposts[color]
	}
	return
}

func (p *Position) attacks(square int) Bitmask {
	return p.attacksFor(square, p.pieces[square])
}

func (p *Position) attacksFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Pawn:
		return pawnMoves[color][square]
	case Knight:
		return knightMoves[square]
	case Bishop:
		return p.bishopMoves(square)
	case Rook:
		return p.rookMoves(square)
	case Queen:
		return p.bishopMoves(square) | p.rookMoves(square)
	case King:
		return kingMoves[square]
	}
	return
}

func (p *Position) xrayAttacks(square int) Bitmask {
	return p.xrayAttacksFor(square, p.pieces[square])
}

func (p *Position) xrayAttacksFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Bishop:
		board := p.board ^ p.outposts[queen(color)]
		return p.bishopMovesAt(square, board)
	case Rook:
		board := p.board ^ p.outposts[rook(color)] ^ p.outposts[queen(color)]
		return p.rookMovesAt(square, board)
	}
	return p.attacksFor(square, piece)
}

func (p *Position) allAttacks(color int) (bitmask Bitmask) {
	bitmask = p.pawnAttacks(color) | p.knightAttacks(color) | p.kingAttacks(color)

	outposts := p.outposts[bishop(color)] | p.outposts[queen(color)]
	for outposts != 0 {
		bitmask |= p.bishopMoves(outposts.pop())
	}

	outposts = p.outposts[rook(color)] | p.outposts[queen(color)]
	for outposts != 0 {
		bitmask |= p.rookMoves(outposts.pop())
	}
	return
}

// Returns a bitmask of pieces that attack given square. The resulting bitmask
// only counts pieces of requested color.
//
// This method is used in static exchange evaluation so instead of using current
// board bitmask (p.board) we pass the one that gets continuously updated during
// the evaluation.
func (p *Position) attackers(square, color int, board Bitmask) Bitmask {
	attackers := knightMoves[square] & p.outposts[knight(color)]
	attackers |= maskPawn[color][square] & p.outposts[pawn(color)]
	attackers |= kingMoves[square] & p.outposts[king(color)]
	attackers |= p.rookMovesAt(square, board) & (p.outposts[rook(color)] | p.outposts[queen(color)])
	attackers |= p.bishopMovesAt(square, board) & (p.outposts[bishop(color)] | p.outposts[queen(color)])

	return attackers
}

func (p *Position) isAttacked(square, color int) bool {
	return (knightMoves[square] & p.outposts[knight(color)]) != 0 ||
		(maskPawn[color][square] & p.outposts[pawn(color)]) != 0 ||
		(kingMoves[square] & p.outposts[king(color)]) != 0 ||
		(p.rookMoves(square) & (p.outposts[rook(color)]|p.outposts[queen(color)])) != 0 ||
		(p.bishopMoves(square) & (p.outposts[bishop(color)]|p.outposts[queen(color)])) != 0
}

func (p *Position) pawnAttacks(color int) (bitmask Bitmask) {
	if color == White {
		bitmask = (p.outposts[Pawn] & ^maskFile[0]) << 7
		bitmask |= (p.outposts[Pawn] & ^maskFile[7]) << 9
	} else {
		bitmask = (p.outposts[BlackPawn] & ^maskFile[0]) >> 9
		bitmask |= (p.outposts[BlackPawn] & ^maskFile[7]) >> 7
	}
	return
}

func (p *Position) knightAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[knight(color)]
	for outposts != 0 {
		bitmask |= knightMoves[outposts.pop()]
	}
	return
}

func (p *Position) bishopAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[bishop(color)]
	for outposts != 0 {
		bitmask |= p.bishopMoves(outposts.pop())
	}
	return
}

func (p *Position) rookAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[rook(color)]
	for outposts != 0 {
		bitmask |= p.rookMoves(outposts.pop())
	}
	return
}

func (p *Position) queenAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[queen(color)]
	for outposts != 0 {
		square := outposts.pop()
		bitmask |= p.rookMoves(square) | p.bishopMoves(square)
	}
	return
}

func (p *Position) kingAttacks(color int) Bitmask {
	return kingMoves[p.king[color]]
}

func (p *Position) strongestPiece(color int, targets Bitmask) Piece {
	if targets & p.outposts[queen(color)] != 0 {
		return queen(color)
	}
	if targets & p.outposts[rook(color)] != 0 {
		return rook(color)
	}
	if targets & p.outposts[bishop(color)] != 0 {
		return bishop(color)
	}
	if targets & p.outposts[knight(color)] != 0 {
		return knight(color)
	}
	if targets & p.outposts[pawn(color)] != 0 {
		return pawn(color)
	}
	return Piece(0)
}<commit_msg>Faster pawn moves<commit_after>// Copyright (c) 2013-2014 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

// Returns a bitmask of possible Bishop moves from the given square.
func (p *Position) bishopMoves(square int) Bitmask {
	return p.bishopMovesAt(square, p.board)
}

// Returns a bitmask of possible Rook moves from the given square.
func (p *Position) rookMoves(square int) Bitmask {
	return p.rookMovesAt(square, p.board)
}

// Returns a bitmask of possible Bishop moves from the given square wherees
// other pieces on the board are represented by the explicit parameter.
func (p *Position) bishopMovesAt(square int, board Bitmask) Bitmask {
	magic := ((bishopMagic[square].mask & board) * bishopMagic[square].magic) >> 55
	return bishopMagicMoves[square][magic]
}

// Returns a bitmask of possible Rook moves from the given square wherees other
// pieces on the board are represented by the explicit parameter.
func (p *Position) rookMovesAt(square int, board Bitmask) Bitmask {
	magic := ((rookMagic[square].mask & board) * rookMagic[square].magic) >> 52
	return rookMagicMoves[square][magic]
}

func (p *Position) targets(square int) Bitmask {
	return p.targetsFor(square, p.pieces[square])
}

func (p *Position) targetsFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Pawn:
		// Start with one square push, then try the second square.
		empty := ^p.board
		if color == White {
			bitmask |= (bit[square] << 8) & empty
			bitmask |= (bitmask << 8) & empty & maskRank[3]
		} else {
			bitmask |= (bit[square] >> 8) & empty
			bitmask |= (bitmask >> 8) & empty & maskRank[4]
		}
		bitmask |= pawnMoves[color][square] & p.outposts[color^1]

		// If the last move set the en-passant square and it is diagonally adjacent
		// to the current pawn, then add en-passant to the pawn's attack targets.
		if p.enpassant != 0 && maskPawn[color][p.enpassant].isSet(square) {
			bitmask.set(p.enpassant)
		}
	case Knight:
		bitmask = knightMoves[square] & ^p.outposts[color]
	case Bishop:
		bitmask = p.bishopMoves(square) & ^p.outposts[color]
	case Rook:
		bitmask = p.rookMoves(square) & ^p.outposts[color]
	case Queen:
		bitmask = (p.bishopMoves(square) | p.rookMoves(square)) & ^p.outposts[color]
	case King:
		bitmask = kingMoves[square] & ^p.outposts[color]
	}
	return
}

func (p *Position) attacks(square int) Bitmask {
	return p.attacksFor(square, p.pieces[square])
}

func (p *Position) attacksFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Pawn:
		return pawnMoves[color][square]
	case Knight:
		return knightMoves[square]
	case Bishop:
		return p.bishopMoves(square)
	case Rook:
		return p.rookMoves(square)
	case Queen:
		return p.bishopMoves(square) | p.rookMoves(square)
	case King:
		return kingMoves[square]
	}
	return
}

func (p *Position) xrayAttacks(square int) Bitmask {
	return p.xrayAttacksFor(square, p.pieces[square])
}

func (p *Position) xrayAttacksFor(square int, piece Piece) (bitmask Bitmask) {
	switch kind, color := piece.kind(), piece.color(); kind {
	case Bishop:
		board := p.board ^ p.outposts[queen(color)]
		return p.bishopMovesAt(square, board)
	case Rook:
		board := p.board ^ p.outposts[rook(color)] ^ p.outposts[queen(color)]
		return p.rookMovesAt(square, board)
	}
	return p.attacksFor(square, piece)
}

func (p *Position) allAttacks(color int) (bitmask Bitmask) {
	bitmask = p.pawnAttacks(color) | p.knightAttacks(color) | p.kingAttacks(color)

	outposts := p.outposts[bishop(color)] | p.outposts[queen(color)]
	for outposts != 0 {
		bitmask |= p.bishopMoves(outposts.pop())
	}

	outposts = p.outposts[rook(color)] | p.outposts[queen(color)]
	for outposts != 0 {
		bitmask |= p.rookMoves(outposts.pop())
	}
	return
}

// Returns a bitmask of pieces that attack given square. The resulting bitmask
// only counts pieces of requested color.
//
// This method is used in static exchange evaluation so instead of using current
// board bitmask (p.board) we pass the one that gets continuously updated during
// the evaluation.
func (p *Position) attackers(square, color int, board Bitmask) Bitmask {
	attackers := knightMoves[square] & p.outposts[knight(color)]
	attackers |= maskPawn[color][square] & p.outposts[pawn(color)]
	attackers |= kingMoves[square] & p.outposts[king(color)]
	attackers |= p.rookMovesAt(square, board) & (p.outposts[rook(color)] | p.outposts[queen(color)])
	attackers |= p.bishopMovesAt(square, board) & (p.outposts[bishop(color)] | p.outposts[queen(color)])

	return attackers
}

func (p *Position) isAttacked(square, color int) bool {
	return (knightMoves[square] & p.outposts[knight(color)]) != 0 ||
		(maskPawn[color][square] & p.outposts[pawn(color)]) != 0 ||
		(kingMoves[square] & p.outposts[king(color)]) != 0 ||
		(p.rookMoves(square) & (p.outposts[rook(color)]|p.outposts[queen(color)])) != 0 ||
		(p.bishopMoves(square) & (p.outposts[bishop(color)]|p.outposts[queen(color)])) != 0
}

func (p *Position) pawnAttacks(color int) (bitmask Bitmask) {
	if color == White {
		bitmask = (p.outposts[Pawn] & ^maskFile[0]) << 7
		bitmask |= (p.outposts[Pawn] & ^maskFile[7]) << 9
	} else {
		bitmask = (p.outposts[BlackPawn] & ^maskFile[0]) >> 9
		bitmask |= (p.outposts[BlackPawn] & ^maskFile[7]) >> 7
	}
	return
}

func (p *Position) knightAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[knight(color)]
	for outposts != 0 {
		bitmask |= knightMoves[outposts.pop()]
	}
	return
}

func (p *Position) bishopAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[bishop(color)]
	for outposts != 0 {
		bitmask |= p.bishopMoves(outposts.pop())
	}
	return
}

func (p *Position) rookAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[rook(color)]
	for outposts != 0 {
		bitmask |= p.rookMoves(outposts.pop())
	}
	return
}

func (p *Position) queenAttacks(color int) (bitmask Bitmask) {
	outposts := p.outposts[queen(color)]
	for outposts != 0 {
		square := outposts.pop()
		bitmask |= p.rookMoves(square) | p.bishopMoves(square)
	}
	return
}

func (p *Position) kingAttacks(color int) Bitmask {
	return kingMoves[p.king[color]]
}

func (p *Position) strongestPiece(color int, targets Bitmask) Piece {
	if targets & p.outposts[queen(color)] != 0 {
		return queen(color)
	}
	if targets & p.outposts[rook(color)] != 0 {
		return rook(color)
	}
	if targets & p.outposts[bishop(color)] != 0 {
		return bishop(color)
	}
	if targets & p.outposts[knight(color)] != 0 {
		return knight(color)
	}
	if targets & p.outposts[pawn(color)] != 0 {
		return pawn(color)
	}
	return Piece(0)
}<|endoftext|>


<commit_before>package getbuild

import (
	"errors"
	"html/template"
	"net/http"

	"github.com/concourse/atc"
	"github.com/concourse/atc/web"
	"github.com/concourse/atc/web/group"
	"github.com/concourse/go-concourse/concourse"
	"github.com/pivotal-golang/lager"
)

type TemplateData struct {
	GroupStates []group.State
	Job         atc.Job
	Builds      []atc.Build

	Build        atc.Build
	Inputs       []atc.PublicBuildInput
	PipelineName string
}

func getNames(r *http.Request) (string, string, string, error) {
	pipelineName := r.FormValue(":pipeline_name")
	jobName := r.FormValue(":job")
	buildName := r.FormValue(":build")

	if len(pipelineName) == 0 || len(jobName) == 0 || len(buildName) == 0 {
		return pipelineName, jobName, buildName, errors.New("Missing required parameters")
	}

	return pipelineName, jobName, buildName, nil
}

func NewHandler(logger lager.Logger, clientFactory web.ClientFactory, template *template.Template) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		client := clientFactory.Build(r)

		pipelineName, jobName, buildName, err := getNames(r)
		if err != nil {
			logger.Error("failed-to-get-names", err)
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		job, found, err := client.Job(pipelineName, jobName)
		if err != nil {
			logger.Error("failed-to-load-job", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		log := logger.Session("get-build", lager.Data{
			"job":   job.Name,
			"build": buildName,
		})

		requestedBuild, found, err := client.JobBuild(pipelineName, jobName, buildName)
		if err != nil {
			log.Error("get-build-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		buildInputsOutputs, _, err := client.BuildResources(requestedBuild.ID)
		if err != nil {
			log.Error("failed-to-get-build-resources", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		bs, _, _, err := client.JobBuilds(pipelineName, jobName, concourse.Page{})
		if err != nil {
			log.Error("get-all-builds-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		pipeline, _, err := client.Pipeline(pipelineName)
		if err != nil {
			log.Error("get-pipeline-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		templateData := TemplateData{
			GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
				for _, groupJob := range g.Jobs {
					if groupJob == job.Name {
						return true
					}
				}

				return false
			}),

			Job:    job,
			Builds: bs,

			Build:        requestedBuild,
			Inputs:       buildInputsOutputs.Inputs,
			PipelineName: pipelineName,
		}

		err = template.Execute(w, templateData)
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": templateData,
			})
		}
	})
}
<commit_msg>show all builds along the top of build view<commit_after>package getbuild

import (
	"errors"
	"html/template"
	"net/http"

	"github.com/concourse/atc"
	"github.com/concourse/atc/web"
	"github.com/concourse/atc/web/group"
	"github.com/concourse/go-concourse/concourse"
	"github.com/pivotal-golang/lager"
)

type TemplateData struct {
	GroupStates []group.State
	Job         atc.Job
	Builds      []atc.Build

	Build        atc.Build
	Inputs       []atc.PublicBuildInput
	PipelineName string
}

func getNames(r *http.Request) (string, string, string, error) {
	pipelineName := r.FormValue(":pipeline_name")
	jobName := r.FormValue(":job")
	buildName := r.FormValue(":build")

	if len(pipelineName) == 0 || len(jobName) == 0 || len(buildName) == 0 {
		return pipelineName, jobName, buildName, errors.New("Missing required parameters")
	}

	return pipelineName, jobName, buildName, nil
}

func NewHandler(logger lager.Logger, clientFactory web.ClientFactory, template *template.Template) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		client := clientFactory.Build(r)

		pipelineName, jobName, buildName, err := getNames(r)
		if err != nil {
			logger.Error("failed-to-get-names", err)
			w.WriteHeader(http.StatusBadRequest)
			return
		}

		job, found, err := client.Job(pipelineName, jobName)
		if err != nil {
			logger.Error("failed-to-load-job", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		log := logger.Session("get-build", lager.Data{
			"job":   job.Name,
			"build": buildName,
		})

		requestedBuild, found, err := client.JobBuild(pipelineName, jobName, buildName)
		if err != nil {
			log.Error("get-build-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if !found {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		buildInputsOutputs, _, err := client.BuildResources(requestedBuild.ID)
		if err != nil {
			log.Error("failed-to-get-build-resources", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		builds, err := getAllJobBuilds(client, pipelineName, jobName)
		if err != nil {
			log.Error("get-all-builds-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		pipeline, _, err := client.Pipeline(pipelineName)
		if err != nil {
			log.Error("get-pipeline-failed", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		templateData := TemplateData{
			GroupStates: group.States(pipeline.Groups, func(g atc.GroupConfig) bool {
				for _, groupJob := range g.Jobs {
					if groupJob == job.Name {
						return true
					}
				}

				return false
			}),

			Job:    job,
			Builds: builds,

			Build:        requestedBuild,
			Inputs:       buildInputsOutputs.Inputs,
			PipelineName: pipelineName,
		}

		err = template.Execute(w, templateData)
		if err != nil {
			log.Fatal("failed-to-build-template", err, lager.Data{
				"template-data": templateData,
			})
		}
	})
}

func getAllJobBuilds(client concourse.Client, pipelineName string, jobName string) ([]atc.Build, error) {
	builds := []atc.Build{}
	page := &concourse.Page{}

	for page != nil {
		bs, pagination, _, err := client.JobBuilds(pipelineName, jobName, *page)
		if err != nil {
			return nil, err
		}

		builds = append(builds, bs...)
		page = pagination.Next
	}

	return builds, nil
}
<|endoftext|>


<commit_before>package wtserver

import (
	"github.com/btcsuite/btcd/txscript"
	"github.com/lightningnetwork/lnd/watchtower/blob"
	"github.com/lightningnetwork/lnd/watchtower/wtdb"
	"github.com/lightningnetwork/lnd/watchtower/wtpolicy"
	"github.com/lightningnetwork/lnd/watchtower/wtwire"
)

// handleCreateSession processes a CreateSession message from the peer, and returns
// a CreateSessionReply in response. This method will only succeed if no existing
// session info is known about the session id. If an existing session is found,
// the reward address is returned in case the client lost our reply.
func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID,
	req *wtwire.CreateSession) error {

	// TODO(conner): validate accept against policy

	// Query the db for session info belonging to the client's session id.
	existingInfo, err := s.cfg.DB.GetSessionInfo(id)
	switch {

	// We already have a session corresponding to this session id, return an
	// error signaling that it already exists in our database. We return the
	// reward address to the client in case they were not able to process
	// our reply earlier.
	case err == nil:
		log.Debugf("Already have session for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CreateSessionCodeAlreadyExists,
			existingInfo.RewardAddress,
		)

	// Some other database error occurred, return a temporary failure.
	case err != wtdb.ErrSessionNotFound:
		log.Errorf("unable to load session info for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	// Ensure that the requested blob type is supported by our tower.
	if !blob.IsSupportedType(req.BlobType) {
		log.Debugf("Rejecting CreateSession from %s, unsupported blob "+
			"type %s", id, req.BlobType)
		return s.replyCreateSession(
			peer, id, wtwire.CreateSessionCodeRejectBlobType, nil,
		)
	}

	// Now that we've established that this session does not exist in the
	// database, retrieve the sweep address that will be given to the
	// client. This address is to be included by the client when signing
	// sweep transactions destined for this tower, if its negotiated output
	// is not dust.
	rewardAddress, err := s.cfg.NewAddress()
	if err != nil {
		log.Errorf("unable to generate reward addr for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	// Construct the pkscript the client should pay to when signing justice
	// transactions for this session.
	rewardScript, err := txscript.PayToAddrScript(rewardAddress)
	if err != nil {
		log.Errorf("unable to generate reward script for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	// TODO(conner): create invoice for upfront payment

	// Assemble the session info using the agreed upon parameters, reward
	// address, and session id.
	info := wtdb.SessionInfo{
		ID: *id,
		Policy: wtpolicy.Policy{
			BlobType:     req.BlobType,
			MaxUpdates:   req.MaxUpdates,
			RewardBase:   req.RewardBase,
			RewardRate:   req.RewardRate,
			SweepFeeRate: req.SweepFeeRate,
		},
		RewardAddress: rewardScript,
	}

	// Insert the session info into the watchtower's database. If
	// successful, the session will now be ready for use.
	err = s.cfg.DB.InsertSessionInfo(&info)
	if err != nil {
		log.Errorf("unable to create session for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	log.Infof("Accepted session for %s", id)

	return s.replyCreateSession(
		peer, id, wtwire.CodeOK, rewardScript,
	)
}

// replyCreateSession sends a response to a CreateSession from a client. If the
// status code in the reply is OK, the error from the write will be bubbled up.
// Otherwise, this method returns a connection error to ensure we don't continue
// communication with the client.
func (s *Server) replyCreateSession(peer Peer, id *wtdb.SessionID,
	code wtwire.ErrorCode, data []byte) error {

	msg := &wtwire.CreateSessionReply{
		Code: code,
		Data: data,
	}

	err := s.sendMessage(peer, msg)
	if err != nil {
		log.Errorf("unable to send CreateSessionReply to %s", id)
	}

	// Return the write error if the request succeeded.
	if code == wtwire.CodeOK {
		return err
	}

	// Otherwise the request failed, return a connection failure to
	// disconnect the client.
	return &connFailure{
		ID:   *id,
		Code: uint16(code),
	}
}
<commit_msg>watchtower/wtserver/server: only generate addrs for reward sessions<commit_after>package wtserver

import (
	"github.com/btcsuite/btcd/txscript"
	"github.com/lightningnetwork/lnd/watchtower/blob"
	"github.com/lightningnetwork/lnd/watchtower/wtdb"
	"github.com/lightningnetwork/lnd/watchtower/wtpolicy"
	"github.com/lightningnetwork/lnd/watchtower/wtwire"
)

// handleCreateSession processes a CreateSession message from the peer, and returns
// a CreateSessionReply in response. This method will only succeed if no existing
// session info is known about the session id. If an existing session is found,
// the reward address is returned in case the client lost our reply.
func (s *Server) handleCreateSession(peer Peer, id *wtdb.SessionID,
	req *wtwire.CreateSession) error {

	// TODO(conner): validate accept against policy

	// Query the db for session info belonging to the client's session id.
	existingInfo, err := s.cfg.DB.GetSessionInfo(id)
	switch {

	// We already have a session corresponding to this session id, return an
	// error signaling that it already exists in our database. We return the
	// reward address to the client in case they were not able to process
	// our reply earlier.
	case err == nil:
		log.Debugf("Already have session for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CreateSessionCodeAlreadyExists,
			existingInfo.RewardAddress,
		)

	// Some other database error occurred, return a temporary failure.
	case err != wtdb.ErrSessionNotFound:
		log.Errorf("unable to load session info for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	// Ensure that the requested blob type is supported by our tower.
	if !blob.IsSupportedType(req.BlobType) {
		log.Debugf("Rejecting CreateSession from %s, unsupported blob "+
			"type %s", id, req.BlobType)
		return s.replyCreateSession(
			peer, id, wtwire.CreateSessionCodeRejectBlobType, nil,
		)
	}

	// Now that we've established that this session does not exist in the
	// database, retrieve the sweep address that will be given to the
	// client. This address is to be included by the client when signing
	// sweep transactions destined for this tower, if its negotiated output
	// is not dust.
	var rewardScript []byte
	if req.BlobType.Has(blob.FlagReward) {
		rewardAddress, err := s.cfg.NewAddress()
		if err != nil {
			log.Errorf("Unable to generate reward addr for %s: %v",
				id, err)
			return s.replyCreateSession(
				peer, id, wtwire.CodeTemporaryFailure, nil,
			)
		}

		// Construct the pkscript the client should pay to when signing
		// justice transactions for this session.
		rewardScript, err = txscript.PayToAddrScript(rewardAddress)
		if err != nil {
			log.Errorf("Unable to generate reward script for "+
				"%s: %v", id, err)
			return s.replyCreateSession(
				peer, id, wtwire.CodeTemporaryFailure, nil,
			)
		}
	}

	// TODO(conner): create invoice for upfront payment

	// Assemble the session info using the agreed upon parameters, reward
	// address, and session id.
	info := wtdb.SessionInfo{
		ID: *id,
		Policy: wtpolicy.Policy{
			BlobType:     req.BlobType,
			MaxUpdates:   req.MaxUpdates,
			RewardBase:   req.RewardBase,
			RewardRate:   req.RewardRate,
			SweepFeeRate: req.SweepFeeRate,
		},
		RewardAddress: rewardScript,
	}

	// Insert the session info into the watchtower's database. If
	// successful, the session will now be ready for use.
	err = s.cfg.DB.InsertSessionInfo(&info)
	if err != nil {
		log.Errorf("unable to create session for %s", id)
		return s.replyCreateSession(
			peer, id, wtwire.CodeTemporaryFailure, nil,
		)
	}

	log.Infof("Accepted session for %s", id)

	return s.replyCreateSession(
		peer, id, wtwire.CodeOK, rewardScript,
	)
}

// replyCreateSession sends a response to a CreateSession from a client. If the
// status code in the reply is OK, the error from the write will be bubbled up.
// Otherwise, this method returns a connection error to ensure we don't continue
// communication with the client.
func (s *Server) replyCreateSession(peer Peer, id *wtdb.SessionID,
	code wtwire.ErrorCode, data []byte) error {

	msg := &wtwire.CreateSessionReply{
		Code: code,
		Data: data,
	}

	err := s.sendMessage(peer, msg)
	if err != nil {
		log.Errorf("unable to send CreateSessionReply to %s", id)
	}

	// Return the write error if the request succeeded.
	if code == wtwire.CodeOK {
		return err
	}

	// Otherwise the request failed, return a connection failure to
	// disconnect the client.
	return &connFailure{
		ID:   *id,
		Code: uint16(code),
	}
}
<|endoftext|>


<commit_before>package installconfig

import (
	survey "gopkg.in/AlecAivazis/survey.v1"

	"github.com/openshift/installer/pkg/asset"
	"github.com/openshift/installer/pkg/validate"
)

type pullSecret struct {
	PullSecret string
}

var _ asset.Asset = (*pullSecret)(nil)

// Dependencies returns no dependencies.
func (a *pullSecret) Dependencies() []asset.Asset {
	return []asset.Asset{}
}

// Generate queries for the pull secret from the user.
func (a *pullSecret) Generate(asset.Parents) error {
	return survey.Ask([]*survey.Question{
		{
			Prompt: &survey.Input{
				Message: "Pull Secret",
				Help:    "The container registry pull secret for this cluster, as a single line of JSON (e.g. {\"auths\": {...}}).\n\nYou can get this secret from https://try.openshift.com",
			},
			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
				return validate.JSON([]byte(ans.(string)))
			}),
		},
	}, &a.PullSecret)
}

// Name returns the human-friendly name of the asset.
func (a *pullSecret) Name() string {
	return "Pull Secret"
}
<commit_msg>pkg/asset/installconfig/pullsecret: Point to cloud.openshift.com<commit_after>package installconfig

import (
	survey "gopkg.in/AlecAivazis/survey.v1"

	"github.com/openshift/installer/pkg/asset"
	"github.com/openshift/installer/pkg/validate"
)

type pullSecret struct {
	PullSecret string
}

var _ asset.Asset = (*pullSecret)(nil)

// Dependencies returns no dependencies.
func (a *pullSecret) Dependencies() []asset.Asset {
	return []asset.Asset{}
}

// Generate queries for the pull secret from the user.
func (a *pullSecret) Generate(asset.Parents) error {
	return survey.Ask([]*survey.Question{
		{
			Prompt: &survey.Input{
				Message: "Pull Secret",
				Help:    "The container registry pull secret for this cluster, as a single line of JSON (e.g. {\"auths\": {...}}).\n\nYou can get this secret from https://cloud.openshift.com/clusters/install#pull-secret",
			},
			Validate: survey.ComposeValidators(survey.Required, func(ans interface{}) error {
				return validate.JSON([]byte(ans.(string)))
			}),
		},
	}, &a.PullSecret)
}

// Name returns the human-friendly name of the asset.
func (a *pullSecret) Name() string {
	return "Pull Secret"
}
<|endoftext|>


<commit_before>package server

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"net/url"
	"time"
)

// KapacitorProxy proxies requests to kapacitor using the path query parameter.
func (h *Service) KapacitorProxy(w http.ResponseWriter, r *http.Request) {
	srcID, err := paramID("id", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
		return
	}

	id, err := paramID("kid", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
		return
	}

	path := r.URL.Query().Get("path")
	if path == "" {
		Error(w, http.StatusUnprocessableEntity, "path query parameter required", h.Logger)
		return
	}

	ctx := r.Context()
	srv, err := h.ServersStore.Get(ctx, id)
	if err != nil || srv.SrcID != srcID {
		notFound(w, id, h.Logger)
		return
	}

	u, err := url.Parse(srv.URL)
	if err != nil {
		msg := fmt.Sprintf("Error parsing kapacitor url: %v", err)
		Error(w, http.StatusUnprocessableEntity, msg, h.Logger)
		return
	}

	u.Path = path

	director := func(req *http.Request) {
		// Set the Host header of the original Kapacitor URL
		req.Host = u.Host

		req.URL = u
		// Because we are acting as a proxy, kapacitor needs to have the basic auth information set as
		// a header directly
		if srv.Username != "" && srv.Password != "" {
			req.SetBasicAuth(srv.Username, srv.Password)
		}
	}

	// Without a FlushInterval the HTTP Chunked response for kapacitor logs is
	// buffered and flushed every 30 seconds.
	proxy := &httputil.ReverseProxy{
		Director:      director,
		FlushInterval: time.Second,
	}
	proxy.ServeHTTP(w, r)
}

// KapacitorProxyPost proxies POST to kapacitor
func (h *Service) KapacitorProxyPost(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyPatch proxies PATCH to kapacitor
func (h *Service) KapacitorProxyPatch(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyGet proxies GET to kapacitor
func (h *Service) KapacitorProxyGet(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyDelete proxies DELETE to kapacitor
func (h *Service) KapacitorProxyDelete(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}
<commit_msg>Fix kapacitor proxy to accept url query parameters<commit_after>package server

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"net/url"
	"strings"
	"time"
)

// KapacitorProxy proxies requests to kapacitor using the path query parameter.
func (h *Service) KapacitorProxy(w http.ResponseWriter, r *http.Request) {
	srcID, err := paramID("id", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
		return
	}

	id, err := paramID("kid", r)
	if err != nil {
		Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
		return
	}

	path := r.URL.Query().Get("path")
	if path == "" {
		Error(w, http.StatusUnprocessableEntity, "path query parameter required", h.Logger)
		return
	}

	ctx := r.Context()
	srv, err := h.ServersStore.Get(ctx, id)
	if err != nil || srv.SrcID != srcID {
		notFound(w, id, h.Logger)
		return
	}

	// To preserve any HTTP query arguments to the kapacitor path,
	// we concat and parse them into u.
	uri := singleJoiningSlash(srv.URL, path)
	u, err := url.Parse(uri)
	if err != nil {
		msg := fmt.Sprintf("Error parsing kapacitor url: %v", err)
		Error(w, http.StatusUnprocessableEntity, msg, h.Logger)
		return
	}

	director := func(req *http.Request) {
		// Set the Host header of the original Kapacitor URL
		req.Host = u.Host
		req.URL = u

		// Because we are acting as a proxy, kapacitor needs to have the basic auth information set as
		// a header directly
		if srv.Username != "" && srv.Password != "" {
			req.SetBasicAuth(srv.Username, srv.Password)
		}
	}

	// Without a FlushInterval the HTTP Chunked response for kapacitor logs is
	// buffered and flushed every 30 seconds.
	proxy := &httputil.ReverseProxy{
		Director:      director,
		FlushInterval: time.Second,
	}
	proxy.ServeHTTP(w, r)
}

// KapacitorProxyPost proxies POST to kapacitor
func (h *Service) KapacitorProxyPost(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyPatch proxies PATCH to kapacitor
func (h *Service) KapacitorProxyPatch(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyGet proxies GET to kapacitor
func (h *Service) KapacitorProxyGet(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

// KapacitorProxyDelete proxies DELETE to kapacitor
func (h *Service) KapacitorProxyDelete(w http.ResponseWriter, r *http.Request) {
	h.KapacitorProxy(w, r)
}

func singleJoiningSlash(a, b string) string {
	aslash := strings.HasSuffix(a, "/")
	bslash := strings.HasPrefix(b, "/")
	if aslash && bslash {
		return a + b[1:]
	}
	if !aslash && !bslash {
		return a + "/" + b
	}
	return a + b
}
<|endoftext|>


<commit_before>package main

import (
	"math/rand"
	"strconv"
)

type FileId string

func GenerateNewFileID() FileId {
	randomNumber := uint64(rand.Int63())

	return FileId(strconv.FormatUint(randomNumber, 36))
}
<commit_msg>delete fileid.go<commit_after><|endoftext|>


<commit_before>package query

import (
	"fmt"

	"../parser"
)

type querier struct {
	root    parser.Todo
	taskMap map[string]task
}

type task struct {
	name     string
	deps     []string
	subtasks []string
}

func makeTaskMap(root parser.Todo) map[string]task {
	t := make(map[string]task)
	addTask(t, root)
	return t
}

func addTask(tmap map[string]task, node interface{}) {
	switch node := node.(type) {
	case parser.Todo:
		for _, decl := range node {
			addTask(tmap, decl)
		}
	case parser.TaskDecl:
		if tmap[node.MainTask.TaskName].name != "" {
			panic("task is declared more than once")
		}

		subtasks := make([]string, len(node.Subtasks))
		for i, e := range node.Subtasks {
			subtasks[i] = e.TaskName
		}

		var t = task{subtasks: subtasks}
		tmap[node.MainTask.TaskName] = t
		addTask(tmap, node.MainTask)
	case parser.Task:
		tname := string(node.TaskName)
		t := tmap[tname]
		t.name = node.TaskName
		if len(node.TaskDeps) > 0 {
			t.deps = make([]string, len(node.TaskDeps))
			for i, str := range node.TaskDeps {
				t.deps[i] = string(str)
			}
		}
		tmap[tname] = t
	}
}

func New(root parser.Todo) querier {
	t := makeTaskMap(root)
	fmt.Println(t)
	return querier{root, t}
}
<commit_msg>Remove useless log.<commit_after>package query

import (
	"../parser"
)

type querier struct {
	root    parser.Todo
	taskMap map[string]task
}

type task struct {
	name     string
	deps     []string
	subtasks []string
}

func makeTaskMap(root parser.Todo) map[string]task {
	t := make(map[string]task)
	addTask(t, root)
	return t
}

func addTask(tmap map[string]task, node interface{}) {
	switch node := node.(type) {
	case parser.Todo:
		for _, decl := range node {
			addTask(tmap, decl)
		}
	case parser.TaskDecl:
		if tmap[node.MainTask.TaskName].name != "" {
			panic("task is declared more than once")
		}

		subtasks := make([]string, len(node.Subtasks))
		for i, e := range node.Subtasks {
			subtasks[i] = e.TaskName
		}

		var t = task{subtasks: subtasks}
		tmap[node.MainTask.TaskName] = t
		addTask(tmap, node.MainTask)
	case parser.Task:
		tname := string(node.TaskName)
		t := tmap[tname]
		t.name = node.TaskName
		if len(node.TaskDeps) > 0 {
			t.deps = make([]string, len(node.TaskDeps))
			for i, str := range node.TaskDeps {
				t.deps[i] = string(str)
			}
		}
		tmap[tname] = t
	}
}

func New(root parser.Todo) querier {
	t := makeTaskMap(root)
	return querier{root, t}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"flag"
	"github.com/Symantec/scotty/pstore"
	"github.com/Symantec/scotty/pstore/influx"
	"github.com/Symantec/scotty/pstore/kafka"
	"github.com/Symantec/tricorder/go/tricorder/types"
	"github.com/Symantec/tricorder/go/tricorder/units"
	"gopkg.in/yaml.v2"
	"log"
	"os"
	"strconv"
	"time"
)

var (
	fKafkaConfigFile = flag.String(
		"kafka_config_file",
		"",
		"kafka configuration file")
	fInfluxConfigFile = flag.String(
		"influx_config_file",
		"",
		"influx configuration file")
	fCommandFile = flag.String(
		"command_file",
		"",
		"command file")
)

type metricMetaData struct {
	HostName string
	AppName  string
	Path     string
	Kind     types.Type
	Unit     units.Unit
}

type metricValue struct {
	Id    *metricMetaData
	Value string
}

type metricCommand struct {
	Values []metricValue
	Sleep  uint
}

func initRecord(value *metricValue, timestamp time.Time, r *pstore.Record) {
	r.HostName = value.Id.HostName
	r.Tags = pstore.TagGroup{pstore.TagAppName: value.Id.AppName}
	r.Path = value.Id.Path
	r.Kind = value.Id.Kind
	r.Unit = value.Id.Unit
	var err error
	switch r.Kind {
	case types.Bool:
		if value.Value == "true" {
			r.Value = true
		} else {
			r.Value = false
		}
	case types.Int8:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int8(intVal)
	case types.Int16:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int16(intVal)
	case types.Int32:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int32(intVal)
	case types.Int64:
		if r.Value, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
	case types.Uint8:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint8(uintVal)
	case types.Uint16:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint16(uintVal)
	case types.Uint32:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint32(uintVal)
	case types.Uint64:
		if r.Value, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
	case types.Float32:
		var floatVal float64
		if floatVal, err = strconv.ParseFloat(value.Value, 64); err != nil {
			panic(err)
		}
		r.Value = float32(floatVal)
	case types.Float64:
		if r.Value, err = strconv.ParseFloat(value.Value, 64); err != nil {
			panic(err)
		}
	case types.String:
		r.Value = value.Value
	case types.GoTime:
		if r.Value, err = time.Parse("01/02/2006 15:04:05", value.Value); err != nil {
			panic(err)
		}
	case types.GoDuration:
		if r.Value, err = time.ParseDuration(value.Value); err != nil {
			panic(err)
		}
	default:
		panic("Unrecognized type")

	}
	r.Timestamp = timestamp
}

func playCommands(
	values []metricValue, timestamp time.Time, writer pstore.RecordWriter) {
	records := make([]pstore.Record, len(values))
	for i := range values {
		initRecord(&values[i], timestamp, &records[i])
	}
	if err := writer.Write(records); err != nil {
		log.Println("Error writing: ", err)
	}
}

func readCommands(commands *[]metricCommand) {
	f, err := os.Open(*fCommandFile)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	var content bytes.Buffer
	if _, err = content.ReadFrom(f); err != nil {
		panic(err)
	}
	if err = yaml.Unmarshal(content.Bytes(), commands); err != nil {
		panic(err)
	}
}

func createKafkaWriter() (result pstore.RecordWriter) {
	var err error
	if *fKafkaConfigFile != "" {
		result, err = kafka.FromFile(*fKafkaConfigFile)
	} else if *fInfluxConfigFile != "" {
		result, err = influx.FromFile(*fInfluxConfigFile)
	} else {
		return kafka.NewFakeWriter()
	}
	if err != nil {
		log.Fatal(err)
	}
	return
}

func main() {
	flag.Parse()
	var commands []metricCommand
	writer := createKafkaWriter()
	readCommands(&commands)
	if len(commands) == 0 {
		log.Fatal("No commands, nothing to do.")
	}
	timestamp := time.Now()
	idx := 0
	for {
		playCommands(commands[idx].Values, timestamp, writer)
		if commands[idx].Sleep <= 0 {
			panic("Need a positive time delay.")
		}
		sleepTime := time.Duration(commands[idx].Sleep) * time.Second
		time.Sleep(sleepTime)
		timestamp = timestamp.Add(sleepTime)
		idx++
		if idx == len(commands) {
			idx = 0
		}
	}
}
<commit_msg>Modify test harness to test open tsdb.<commit_after>package main

import (
	"bytes"
	"flag"
	"github.com/Symantec/scotty/pstore"
	"github.com/Symantec/scotty/pstore/influx"
	"github.com/Symantec/scotty/pstore/kafka"
	"github.com/Symantec/scotty/pstore/tsdb"
	"github.com/Symantec/tricorder/go/tricorder/types"
	"github.com/Symantec/tricorder/go/tricorder/units"
	"gopkg.in/yaml.v2"
	"log"
	"os"
	"strconv"
	"time"
)

var (
	fKafkaConfigFile = flag.String(
		"kafka_config_file",
		"",
		"kafka configuration file")
	fInfluxConfigFile = flag.String(
		"influx_config_file",
		"",
		"influx configuration file")
	fTsdbConfigFile = flag.String(
		"tsdb_config_file",
		"",
		"tsdb configuration file")
	fCommandFile = flag.String(
		"command_file",
		"",
		"command file")
)

type metricMetaData struct {
	HostName string
	AppName  string
	Path     string
	Kind     types.Type
	Unit     units.Unit
}

type metricValue struct {
	Id    *metricMetaData
	Value string
}

type metricCommand struct {
	Values []metricValue
	Sleep  uint
}

func initRecord(value *metricValue, timestamp time.Time, r *pstore.Record) {
	r.HostName = value.Id.HostName
	r.Tags = pstore.TagGroup{pstore.TagAppName: value.Id.AppName}
	r.Path = value.Id.Path
	r.Kind = value.Id.Kind
	r.Unit = value.Id.Unit
	var err error
	switch r.Kind {
	case types.Bool:
		if value.Value == "true" {
			r.Value = true
		} else {
			r.Value = false
		}
	case types.Int8:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int8(intVal)
	case types.Int16:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int16(intVal)
	case types.Int32:
		var intVal int64
		if intVal, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = int32(intVal)
	case types.Int64:
		if r.Value, err = strconv.ParseInt(value.Value, 10, 64); err != nil {
			panic(err)
		}
	case types.Uint8:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint8(uintVal)
	case types.Uint16:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint16(uintVal)
	case types.Uint32:
		var uintVal uint64
		if uintVal, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
		r.Value = uint32(uintVal)
	case types.Uint64:
		if r.Value, err = strconv.ParseUint(value.Value, 10, 64); err != nil {
			panic(err)
		}
	case types.Float32:
		var floatVal float64
		if floatVal, err = strconv.ParseFloat(value.Value, 64); err != nil {
			panic(err)
		}
		r.Value = float32(floatVal)
	case types.Float64:
		if r.Value, err = strconv.ParseFloat(value.Value, 64); err != nil {
			panic(err)
		}
	case types.String:
		r.Value = value.Value
	case types.GoTime:
		if r.Value, err = time.Parse("01/02/2006 15:04:05", value.Value); err != nil {
			panic(err)
		}
	case types.GoDuration:
		if r.Value, err = time.ParseDuration(value.Value); err != nil {
			panic(err)
		}
	default:
		panic("Unrecognized type")

	}
	r.Timestamp = timestamp
}

func playCommands(
	values []metricValue, timestamp time.Time, writer pstore.RecordWriter) {
	records := make([]pstore.Record, len(values))
	for i := range values {
		initRecord(&values[i], timestamp, &records[i])
	}
	if err := writer.Write(records); err != nil {
		log.Println("Error writing: ", err)
	}
}

func readCommands(commands *[]metricCommand) {
	f, err := os.Open(*fCommandFile)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	var content bytes.Buffer
	if _, err = content.ReadFrom(f); err != nil {
		panic(err)
	}
	if err = yaml.Unmarshal(content.Bytes(), commands); err != nil {
		panic(err)
	}
}

func createKafkaWriter() (result pstore.RecordWriter) {
	var err error
	if *fKafkaConfigFile != "" {
		result, err = kafka.FromFile(*fKafkaConfigFile)
	} else if *fInfluxConfigFile != "" {
		result, err = influx.FromFile(*fInfluxConfigFile)
	} else if *fTsdbConfigFile != "" {
		result, err = tsdb.FromFile(*fTsdbConfigFile)
	} else {
		return kafka.NewFakeWriter()
	}
	if err != nil {
		log.Fatal(err)
	}
	return
}

func main() {
	flag.Parse()
	var commands []metricCommand
	writer := createKafkaWriter()
	readCommands(&commands)
	if len(commands) == 0 {
		log.Fatal("No commands, nothing to do.")
	}
	timestamp := time.Now()
	idx := 0
	for {
		playCommands(commands[idx].Values, timestamp, writer)
		if commands[idx].Sleep <= 0 {
			panic("Need a positive time delay.")
		}
		sleepTime := time.Duration(commands[idx].Sleep) * time.Second
		time.Sleep(sleepTime)
		timestamp = timestamp.Add(sleepTime)
		idx++
		if idx == len(commands) {
			idx = 0
		}
	}
}
<|endoftext|>


<commit_before>package javascript

import (
	"regexp"

	"github.com/anchore/syft/internal/log"
	"github.com/anchore/syft/syft/artifact"
	"github.com/anchore/syft/syft/pkg"
	"github.com/anchore/syft/syft/pkg/cataloger/generic"
	"github.com/anchore/syft/syft/source"
)

var nodeClassifier = generic.Classifier{
	Package: "node.js", // Note: this purposely matches the "node.js" string to aid nvd vuln matching
	FilepathPatterns: []*regexp.Regexp{
		// note: should we just parse all files resolved with executable mimetypes
		// regexp that matches node binary
		regexp.MustCompile(`(.*/|^)node$`),
	},
	EvidencePatterns: []*regexp.Regexp{
		// regex that matches node.js/vx.y.z
		regexp.MustCompile(`(?m)node\.js\/v(?P<version>[0-9]+\.[0-9]+\.[0-9]+)`),
	},
	CPEs: []pkg.CPE{
		pkg.MustCPE("cpe:2.3:a:nodejs:node.js:*:*:*:*:*:*:*:*"),
	},
}

func parseNodeBinary(_ source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
	p, _, err := nodeClassifier.Examine(reader)
	if err != nil {
		log.Trace("failed to find node.js package: %+v", err)
		return nil, nil, nil // we can silently fail here to reduce warning noise
	}

	// TODO add node specific metadata to the packages to help with vulnerability matching
	if p != nil {
		p.Language = pkg.JavaScript
		return []pkg.Package{*p}, nil, nil
	}
	p.SetID()
	return nil, nil, nil
}
<commit_msg>javascript cataloger: node binary: nil pointer dereference (#1313)<commit_after>package javascript

import (
	"regexp"

	"github.com/anchore/syft/internal/log"
	"github.com/anchore/syft/syft/artifact"
	"github.com/anchore/syft/syft/pkg"
	"github.com/anchore/syft/syft/pkg/cataloger/generic"
	"github.com/anchore/syft/syft/source"
)

var nodeClassifier = generic.Classifier{
	Package: "node.js", // Note: this purposely matches the "node.js" string to aid nvd vuln matching
	FilepathPatterns: []*regexp.Regexp{
		// note: should we just parse all files resolved with executable mimetypes
		// regexp that matches node binary
		regexp.MustCompile(`(.*/|^)node$`),
	},
	EvidencePatterns: []*regexp.Regexp{
		// regex that matches node.js/vx.y.z
		regexp.MustCompile(`(?m)node\.js\/v(?P<version>[0-9]+\.[0-9]+\.[0-9]+)`),
	},
	CPEs: []pkg.CPE{
		pkg.MustCPE("cpe:2.3:a:nodejs:node.js:*:*:*:*:*:*:*:*"),
	},
}

func parseNodeBinary(_ source.FileResolver, _ *generic.Environment, reader source.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
	p, _, err := nodeClassifier.Examine(reader)
	if err != nil {
		log.Trace("failed to find node.js package: %+v", err)
		return nil, nil, nil // we can silently fail here to reduce warning noise
	}

	// TODO add node specific metadata to the packages to help with vulnerability matching
	if p != nil {
		p.Language = pkg.JavaScript
		p.SetID()
		return []pkg.Package{*p}, nil, nil
	}
	return nil, nil, nil
}
<|endoftext|>


<commit_before>package libkbfs

import (
	"reflect"
	"testing"
	"time"

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

func totalBlockRefs(m map[BlockID]map[BlockRefNonce]blockRefLocalStatus) int {
	n := 0
	for _, refs := range m {
		n += len(refs)
	}
	return n
}

// Test that quota reclamation works for a simple case where the user
// does a few updates, then lets quota reclamation run, and we make
// sure that all historical blocks have been deleted.
func TestQuotaReclamationSimple(t *testing.T) {
	var userName libkb.NormalizedUsername = "test_user"
	config, _, ctx := kbfsOpsInitNoMocks(t, userName)
	defer CheckConfigAndShutdown(t, config)

	now := time.Now()
	clock := &TestClock{now}
	config.SetClock(clock)

	kbfsOps := config.KBFSOps()
	rootNode, _, err :=
		kbfsOps.GetOrCreateRootNode(ctx, userName.String(), false, MasterBranch)
	if err != nil {
		t.Fatalf("Couldn't create folder: %v", err)
	}
	_, _, err = kbfsOps.CreateDir(ctx, rootNode, "a")
	if err != nil {
		t.Fatalf("Couldn't create dir: %v", err)
	}
	err = kbfsOps.RemoveDir(ctx, rootNode, "a")
	if err != nil {
		t.Fatalf("Couldn't remove dir: %v", err)
	}

	// Wait for outstanding archives
	err = kbfsOps.SyncFromServer(ctx, rootNode.GetFolderBranch())
	if err != nil {
		t.Fatalf("Couldn't sync from server: %v", err)
	}

	// Make sure no blocks are deleted before there's a new-enough update.
	bserverLocal, ok := config.BlockServer().(*BlockServerLocal)
	if !ok {
		t.Fatalf("Bad block server")
	}
	preQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	ops := kbfsOps.(*KBFSOpsStandard).getOpsByNode(rootNode)
	ops.fbm.forceQuotaReclamation()
	err = ops.fbm.waitForQuotaReclamations(ctx)
	if err != nil {
		t.Fatalf("Couldn't wait for QR: %v", err)
	}

	postQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	if !reflect.DeepEqual(preQR1Blocks, postQR1Blocks) {
		t.Fatalf("Blocks deleted too early (%v vs %v)!",
			preQR1Blocks, postQR1Blocks)
	}

	// Increase the time and make a new revision, but don't run quota
	// reclamation yet.
	clock.T = now.Add(2 * config.QuotaReclamationMinUnrefAge())
	_, _, err = kbfsOps.CreateDir(ctx, rootNode, "b")
	if err != nil {
		t.Fatalf("Couldn't create dir: %v", err)
	}

	preQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	ops.fbm.forceQuotaReclamation()
	err = ops.fbm.waitForQuotaReclamations(ctx)
	if err != nil {
		t.Fatalf("Couldn't wait for QR: %v", err)
	}

	postQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	if pre, post := totalBlockRefs(preQR2Blocks),
		totalBlockRefs(postQR2Blocks); post >= pre {
		t.Errorf("Blocks didn't shrink after reclamation: pre: %d, post %d",
			pre, post)
	}
}
<commit_msg>folder_block_manager_test: test incremental reclamation<commit_after>package libkbfs

import (
	"reflect"
	"testing"
	"time"

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

func totalBlockRefs(m map[BlockID]map[BlockRefNonce]blockRefLocalStatus) int {
	n := 0
	for _, refs := range m {
		n += len(refs)
	}
	return n
}

// Test that quota reclamation works for a simple case where the user
// does a few updates, then lets quota reclamation run, and we make
// sure that all historical blocks have been deleted.
func TestQuotaReclamationSimple(t *testing.T) {
	var userName libkb.NormalizedUsername = "test_user"
	config, _, ctx := kbfsOpsInitNoMocks(t, userName)
	defer CheckConfigAndShutdown(t, config)

	now := time.Now()
	clock := &TestClock{now}
	config.SetClock(clock)

	kbfsOps := config.KBFSOps()
	rootNode, _, err :=
		kbfsOps.GetOrCreateRootNode(ctx, userName.String(), false, MasterBranch)
	if err != nil {
		t.Fatalf("Couldn't create folder: %v", err)
	}
	_, _, err = kbfsOps.CreateDir(ctx, rootNode, "a")
	if err != nil {
		t.Fatalf("Couldn't create dir: %v", err)
	}
	err = kbfsOps.RemoveDir(ctx, rootNode, "a")
	if err != nil {
		t.Fatalf("Couldn't remove dir: %v", err)
	}

	// Wait for outstanding archives
	err = kbfsOps.SyncFromServer(ctx, rootNode.GetFolderBranch())
	if err != nil {
		t.Fatalf("Couldn't sync from server: %v", err)
	}

	// Make sure no blocks are deleted before there's a new-enough update.
	bserverLocal, ok := config.BlockServer().(*BlockServerLocal)
	if !ok {
		t.Fatalf("Bad block server")
	}
	preQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	ops := kbfsOps.(*KBFSOpsStandard).getOpsByNode(rootNode)
	ops.fbm.forceQuotaReclamation()
	err = ops.fbm.waitForQuotaReclamations(ctx)
	if err != nil {
		t.Fatalf("Couldn't wait for QR: %v", err)
	}

	postQR1Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	if !reflect.DeepEqual(preQR1Blocks, postQR1Blocks) {
		t.Fatalf("Blocks deleted too early (%v vs %v)!",
			preQR1Blocks, postQR1Blocks)
	}

	// Increase the time and make a new revision, but don't run quota
	// reclamation yet.
	clock.T = now.Add(2 * config.QuotaReclamationMinUnrefAge())
	_, _, err = kbfsOps.CreateDir(ctx, rootNode, "b")
	if err != nil {
		t.Fatalf("Couldn't create dir: %v", err)
	}

	preQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	ops.fbm.forceQuotaReclamation()
	err = ops.fbm.waitForQuotaReclamations(ctx)
	if err != nil {
		t.Fatalf("Couldn't wait for QR: %v", err)
	}

	postQR2Blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	if pre, post := totalBlockRefs(preQR2Blocks),
		totalBlockRefs(postQR2Blocks); post >= pre {
		t.Errorf("Blocks didn't shrink after reclamation: pre: %d, post %d",
			pre, post)
	}
}

// Test that a single quota reclamation run doesn't try to reclaim too
// much quota at once.
func TestQuotaReclamationIncrementalReclamation(t *testing.T) {
	var userName libkb.NormalizedUsername = "test_user"
	config, _, ctx := kbfsOpsInitNoMocks(t, userName)
	defer CheckConfigAndShutdown(t, config)

	now := time.Now()
	clock := &TestClock{now}
	config.SetClock(clock)

	kbfsOps := config.KBFSOps()
	rootNode, _, err :=
		kbfsOps.GetOrCreateRootNode(ctx, userName.String(), false, MasterBranch)
	if err != nil {
		t.Fatalf("Couldn't create folder: %v", err)
	}
	// Do a bunch of operations.
	for i := 0; i < numPointersPerGCThreshold; i++ {
		_, _, err = kbfsOps.CreateDir(ctx, rootNode, "a")
		if err != nil {
			t.Fatalf("Couldn't create dir: %v", err)
		}
		err = kbfsOps.RemoveDir(ctx, rootNode, "a")
		if err != nil {
			t.Fatalf("Couldn't remove dir: %v", err)
		}
	}

	// Increase the time, and make sure that there is still more than
	// one block in the history
	clock.T = now.Add(2 * config.QuotaReclamationMinUnrefAge())

	// Run it.
	ops := kbfsOps.(*KBFSOpsStandard).getOpsByNode(rootNode)
	ops.fbm.forceQuotaReclamation()
	err = ops.fbm.waitForQuotaReclamations(ctx)
	if err != nil {
		t.Fatalf("Couldn't wait for QR: %v", err)
	}

	bserverLocal, ok := config.BlockServer().(*BlockServerLocal)
	if !ok {
		t.Fatalf("Bad block server")
	}
	blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
	if err != nil {
		t.Fatalf("Couldn't get blocks: %v", err)
	}

	b := totalBlockRefs(blocks)
	if b <= 1 {
		t.Errorf("Too many blocks left after first QR: %d", b)
	}

	// Now let it run to completion
	for b > 1 {
		ops.fbm.forceQuotaReclamation()
		err = ops.fbm.waitForQuotaReclamations(ctx)
		if err != nil {
			t.Fatalf("Couldn't wait for QR: %v", err)
		}

		blocks, err := bserverLocal.getAll(rootNode.GetFolderBranch().Tlf)
		if err != nil {
			t.Fatalf("Couldn't get blocks: %v", err)
		}
		oldB := b
		b = totalBlockRefs(blocks)
		if b >= oldB {
			t.Fatalf("Blocks didn't shrink after reclamation: %d vs. %d",
				b, oldB)
		}
	}
}
<|endoftext|>


<commit_before>package client

import (
	"fmt"
	"io"
	"os"
	"time"

	"strings"

	"github.com/ncodes/cocoon/core/orderer"
	proto "github.com/ncodes/cocoon/core/runtime/golang/proto"
	"github.com/ncodes/cocoon/core/types"
	logging "github.com/op/go-logging"
	cmap "github.com/orcaman/concurrent-map"
	context "golang.org/x/net/context"
	"google.golang.org/grpc"
)

var log = logging.MustGetLogger("connector.client")

// txChannels holds the channels to send transaction responses to
var txRespChannels = cmap.New()

// Client represents a cocoon code GRPC client
// that interacts with a cocoon code.
type Client struct {
	ccodeAddr        string
	stub             proto.StubClient
	conCtx           context.Context
	conCancel        context.CancelFunc
	orderDiscoTicker *time.Ticker
	ordererAddrs     []string
	stream           proto.Stub_TransactClient
	cocoonID         string
}

// NewClient creates a new cocoon code client
func NewClient() *Client {
	return &Client{}
}

// SetCocoonID sets the cocoon id
func (c *Client) SetCocoonID(id string) {
	c.cocoonID = id
}

// SetCocoonCodeAddr sets the cocoon code bind address
func (c *Client) SetCocoonCodeAddr(ccAddr string) {
	c.ccodeAddr = ccAddr
}

// getCCAddr returns the cocoon code bind address.
// For development, if DEV_COCOON_ADDR is set, it connects to it.
func (c *Client) getCCAddr() string {
	if devCCodeAddr := os.Getenv("DEV_COCOON_ADDR"); len(devCCodeAddr) > 0 {
		return devCCodeAddr
	}
	return c.ccodeAddr
}

// Close the stream and cancel connections
func (c *Client) Close() {
	if c.stream != nil {
		c.stream.CloseSend()
	}
	if c.conCancel != nil {
		c.conCancel()
	}
}

// GetStream returns the grpc stream connected to the grpc cocoon code server
func (c *Client) GetStream() proto.Stub_TransactClient {
	return c.stream
}

// Connect connects to a cocoon code server
// running on a known port
func (c *Client) Connect() error {

	log.Info("Starting cocoon code client")

	// start a ticker to continously discover orderer addreses
	go func() {
		c.orderDiscoTicker = time.NewTicker(60 * time.Second)
		for _ = range c.orderDiscoTicker.C {
			var ordererAddrs []string
			ordererAddrs, err := orderer.DiscoverOrderers()
			if err != nil {
				log.Error(err.Error())
				continue
			}
			c.ordererAddrs = ordererAddrs
		}
	}()

	var ordererAddrs []string
	ordererAddrs, err := orderer.DiscoverOrderers()
	if err != nil {
		return err
	}
	c.ordererAddrs = ordererAddrs

	if len(c.ordererAddrs) > 0 {
		log.Infof("Orderer address list updated. Contains %d orderer address(es)", len(c.ordererAddrs))
	} else {
		log.Warning("No orderer address was found. We won't be able to reach the orderer. ")
	}

	// conn, err := grpc.Dial(c.getCCAddr(), grpc.WithInsecure())
	// if err != nil {
	// 	return fmt.Errorf("failed to connect to cocoon code server. %s", err)
	// }
	// defer conn.Close()

	// log.Debugf("Now connected to cocoon code at port=%s", strings.Split(c.getCCAddr(), ":")[1])

	// c.stub = proto.NewStubClient(conn)

	// if err = c.Do(conn); err != nil {
	// 	return err
	// }
	s := make(chan bool)
	<-s

	return nil
}

// Do starts a request loop that continously asks the
// server for transactions. When it receives a transaction,
// it processes it and returns the result.
func (c *Client) Do(conn *grpc.ClientConn) error {

	var err error

	// create a context so we have complete controll of the connection
	c.conCtx, c.conCancel = context.WithCancel(context.Background())

	// connect to the cocoon code
	c.stream, err = c.stub.Transact(c.conCtx)
	if err != nil {
		return fmt.Errorf("failed to start transaction stream with cocoon code. %s", err)
	}

	for {

		in, err := c.stream.Recv()
		if err == io.EOF {
			return fmt.Errorf("connection with cocoon code has ended")
		}
		if err != nil {
			if strings.Contains(err.Error(), "context canceled") {
				log.Info("Connection to cocoon code closed")
				return nil
			}
			return fmt.Errorf("failed to read message from cocoon code. %s", err)
		}

		switch in.Invoke {
		case true:
			go func() {
				log.Debugf("New invoke transaction (%s) from cocoon code", in.GetId())
				if err = c.handleInvokeTransaction(in); err != nil {
					log.Error(err.Error())
					c.stream.Send(&proto.Tx{
						Response: true,
						Id:       in.GetId(),
						Status:   500,
						Body:     []byte(err.Error()),
					})
				}
			}()
		case false:
			log.Debugf("New response transaction (%s) from cocoon code", in.GetId())
			go func() {
				if err = c.handleRespTransaction(in); err != nil {
					log.Error(err.Error())
					c.stream.Send(&proto.Tx{
						Response: true,
						Id:       in.GetId(),
						Status:   500,
						Body:     []byte(err.Error()),
					})
				}
			}()
		}
	}
}

// handleInvokeTransaction processes invoke transaction requests
func (c *Client) handleInvokeTransaction(tx *proto.Tx) error {
	switch tx.GetName() {
	case types.TxCreateLedger:
		return c.createLedger(tx)
	case types.TxPut:
		return c.put(tx)
	case types.TxGetLedger:
		return c.getLedger(tx)
	case types.TxGet:
		return c.get(tx, false)
	case types.TxGetByID:
		return c.get(tx, true)
	case types.TxGetBlockByID:
		return c.getBlock(tx)
	case types.TxRangeGet:
		return c.getRange(tx)
	default:
		return c.stream.Send(&proto.Tx{
			Id:       tx.GetId(),
			Response: true,
			Status:   500,
			Body:     []byte(fmt.Sprintf("unsupported transaction name (%s)", tx.GetName())),
		})
	}
}

// handleRespTransaction passes the transaction to a response
// channel with a matching transaction id and deletes the channel afterwards.
func (c *Client) handleRespTransaction(tx *proto.Tx) error {
	if !txRespChannels.Has(tx.GetId()) {
		return fmt.Errorf("response transaction (%s) does not have a corresponding response channel", tx.GetId())
	}

	txRespCh, _ := txRespChannels.Get(tx.GetId())
	txRespCh.(chan *proto.Tx) <- tx
	txRespChannels.Remove(tx.GetId())
	return nil
}

// SendTx sends a transaction to the cocoon code
// and saves the response channel. The response channel will
// be passed a response when it is available in the Transact loop.
func (c *Client) SendTx(tx *proto.Tx, respCh chan *proto.Tx) error {
	txRespChannels.Set(tx.GetId(), respCh)
	if err := c.stream.Send(tx); err != nil {
		txRespChannels.Remove(tx.GetId())
		return err
	}
	log.Debugf("Successfully sent transaction (%s) to cocoon code", tx.GetId())
	return nil
}
<commit_msg>debug<commit_after>package client

import (
	"fmt"
	"io"
	"os"
	"time"

	"strings"

	"github.com/ncodes/cocoon/core/orderer"
	proto "github.com/ncodes/cocoon/core/runtime/golang/proto"
	"github.com/ncodes/cocoon/core/types"
	logging "github.com/op/go-logging"
	cmap "github.com/orcaman/concurrent-map"
	context "golang.org/x/net/context"
	"google.golang.org/grpc"
)

var log = logging.MustGetLogger("connector.client")

// txChannels holds the channels to send transaction responses to
var txRespChannels = cmap.New()

// Client represents a cocoon code GRPC client
// that interacts with a cocoon code.
type Client struct {
	ccodeAddr        string
	stub             proto.StubClient
	conCtx           context.Context
	conCancel        context.CancelFunc
	orderDiscoTicker *time.Ticker
	ordererAddrs     []string
	stream           proto.Stub_TransactClient
	cocoonID         string
}

// NewClient creates a new cocoon code client
func NewClient() *Client {
	return &Client{}
}

// SetCocoonID sets the cocoon id
func (c *Client) SetCocoonID(id string) {
	c.cocoonID = id
}

// SetCocoonCodeAddr sets the cocoon code bind address
func (c *Client) SetCocoonCodeAddr(ccAddr string) {
	c.ccodeAddr = ccAddr
}

// getCCAddr returns the cocoon code bind address.
// For development, if DEV_COCOON_ADDR is set, it connects to it.
func (c *Client) getCCAddr() string {
	if devCCodeAddr := os.Getenv("DEV_COCOON_ADDR"); len(devCCodeAddr) > 0 {
		return devCCodeAddr
	}
	return c.ccodeAddr
}

// Close the stream and cancel connections
func (c *Client) Close() {
	if c.stream != nil {
		c.stream.CloseSend()
	}
	if c.conCancel != nil {
		c.conCancel()
	}
}

// GetStream returns the grpc stream connected to the grpc cocoon code server
func (c *Client) GetStream() proto.Stub_TransactClient {
	return c.stream
}

// Connect connects to a cocoon code server
// running on a known port
func (c *Client) Connect() error {

	log.Info("Starting cocoon code client")

	// start a ticker to continously discover orderer addreses
	go func() {
		c.orderDiscoTicker = time.NewTicker(60 * time.Second)
		for _ = range c.orderDiscoTicker.C {
			var ordererAddrs []string
			ordererAddrs, err := orderer.DiscoverOrderers()
			if err != nil {
				log.Error(err.Error())
				continue
			}
			c.ordererAddrs = ordererAddrs
		}
	}()

	var ordererAddrs []string
	ordererAddrs, err := orderer.DiscoverOrderers()
	if err != nil {
		return err
	}
	c.ordererAddrs = ordererAddrs

	if len(c.ordererAddrs) > 0 {
		log.Infof("Orderer address list updated. Contains %d orderer address(es)", len(c.ordererAddrs))
	} else {
		log.Warning("No orderer address was found. We won't be able to reach the orderer. ")
	}

	conn, err := grpc.Dial(c.getCCAddr(), grpc.WithInsecure())
	if err != nil {
		return fmt.Errorf("failed to connect to cocoon code server. %s", err)
	}
	defer conn.Close()

	log.Debugf("Now connected to cocoon code at port=%s", strings.Split(c.getCCAddr(), ":")[1])

	// c.stub = proto.NewStubClient(conn)

	// if err = c.Do(conn); err != nil {
	// 	return err
	// }
	s := make(chan bool)
	<-s

	return nil
}

// Do starts a request loop that continously asks the
// server for transactions. When it receives a transaction,
// it processes it and returns the result.
func (c *Client) Do(conn *grpc.ClientConn) error {

	var err error

	// create a context so we have complete controll of the connection
	c.conCtx, c.conCancel = context.WithCancel(context.Background())

	// connect to the cocoon code
	c.stream, err = c.stub.Transact(c.conCtx)
	if err != nil {
		return fmt.Errorf("failed to start transaction stream with cocoon code. %s", err)
	}

	for {

		in, err := c.stream.Recv()
		if err == io.EOF {
			return fmt.Errorf("connection with cocoon code has ended")
		}
		if err != nil {
			if strings.Contains(err.Error(), "context canceled") {
				log.Info("Connection to cocoon code closed")
				return nil
			}
			return fmt.Errorf("failed to read message from cocoon code. %s", err)
		}

		switch in.Invoke {
		case true:
			go func() {
				log.Debugf("New invoke transaction (%s) from cocoon code", in.GetId())
				if err = c.handleInvokeTransaction(in); err != nil {
					log.Error(err.Error())
					c.stream.Send(&proto.Tx{
						Response: true,
						Id:       in.GetId(),
						Status:   500,
						Body:     []byte(err.Error()),
					})
				}
			}()
		case false:
			log.Debugf("New response transaction (%s) from cocoon code", in.GetId())
			go func() {
				if err = c.handleRespTransaction(in); err != nil {
					log.Error(err.Error())
					c.stream.Send(&proto.Tx{
						Response: true,
						Id:       in.GetId(),
						Status:   500,
						Body:     []byte(err.Error()),
					})
				}
			}()
		}
	}
}

// handleInvokeTransaction processes invoke transaction requests
func (c *Client) handleInvokeTransaction(tx *proto.Tx) error {
	switch tx.GetName() {
	case types.TxCreateLedger:
		return c.createLedger(tx)
	case types.TxPut:
		return c.put(tx)
	case types.TxGetLedger:
		return c.getLedger(tx)
	case types.TxGet:
		return c.get(tx, false)
	case types.TxGetByID:
		return c.get(tx, true)
	case types.TxGetBlockByID:
		return c.getBlock(tx)
	case types.TxRangeGet:
		return c.getRange(tx)
	default:
		return c.stream.Send(&proto.Tx{
			Id:       tx.GetId(),
			Response: true,
			Status:   500,
			Body:     []byte(fmt.Sprintf("unsupported transaction name (%s)", tx.GetName())),
		})
	}
}

// handleRespTransaction passes the transaction to a response
// channel with a matching transaction id and deletes the channel afterwards.
func (c *Client) handleRespTransaction(tx *proto.Tx) error {
	if !txRespChannels.Has(tx.GetId()) {
		return fmt.Errorf("response transaction (%s) does not have a corresponding response channel", tx.GetId())
	}

	txRespCh, _ := txRespChannels.Get(tx.GetId())
	txRespCh.(chan *proto.Tx) <- tx
	txRespChannels.Remove(tx.GetId())
	return nil
}

// SendTx sends a transaction to the cocoon code
// and saves the response channel. The response channel will
// be passed a response when it is available in the Transact loop.
func (c *Client) SendTx(tx *proto.Tx, respCh chan *proto.Tx) error {
	txRespChannels.Set(tx.GetId(), respCh)
	if err := c.stream.Send(tx); err != nil {
		txRespChannels.Remove(tx.GetId())
		return err
	}
	log.Debugf("Successfully sent transaction (%s) to cocoon code", tx.GetId())
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"crypto/sha256"
	"flag"
	"fmt"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"io/ioutil"
	"os"
	"time"

	"golang.org/x/crypto/ed25519"

	"requestor/randomset"
)

type Certificate struct {
	Host        []byte
	Certificate []byte
	Signature   []byte
}

type SignedDirectory struct {
	Directory []byte
	PublicKey ed25519.PublicKey
	Signature []byte
}

type DirectoryBody struct {
	Verifiers     []DirectoryEntry
	LastDirectory string
	Time          time.Time
	Validity      time.Duration
}

type DirectoryEntry struct {
	Commit          []byte
	Reveal          []byte
	Signature       []byte
}

type VerifierCommit struct {
	JSON        []byte
	Signature   []byte
}

type VerifierReveal struct {
	PublicKey   string
	RevealValue string
}

type Verifier struct {
	PublicKey   ed25519.PublicKey
	Address     string
	Time        time.Time
	CommitValue []byte
}

func main() {
	k := flag.Int("k", 1, "Verifiers needed per certificate")
	flag.Parse()

	args := flag.Args()
	dirfile := args[0]

	fh, err := os.Open(dirfile)
	defer fh.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not open directory file: %s\n", err.Error())
		os.Exit(1)
	}

	data, err := ioutil.ReadAll(fh)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not read directory file.\n")
		os.Exit(1)
	}

	var directory_container SignedDirectory
	err = json.Unmarshal(data, &directory_container)
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not parse directory file: %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("Directory public key: %s\n",
		base64.StdEncoding.EncodeToString(
			directory_container.PublicKey))

	signature_valid := ed25519.Verify(directory_container.PublicKey,
		directory_container.Directory, directory_container.Signature)
	if signature_valid {
		fmt.Println("Signature: valid")
	} else {
		fmt.Println("Signature: invalid")
	}

	var directory DirectoryBody
	err = json.Unmarshal(directory_container.Directory, &directory)
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not parse directory body: %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("Valid %s from %s\n",
		directory.Validity.String(),
		directory.Time.Format("2006-01-02 15:04"))

	fmt.Printf("Found %d verifiers.\n", len(directory.Verifiers))

	var verifier_commit_prev Verifier

	hash_context := sha256.New()
	reveal_valid := true
	for index, verifier_json := range directory.Verifiers {
		var verifier_commit Verifier
		err = json.Unmarshal(verifier_json.Commit, &verifier_commit)
		if err != nil {
			fmt.Println("Verifier: invalid")
			reveal_valid = false
		}

		signature_valid := ed25519.Verify(verifier_commit.PublicKey,
			verifier_json.Commit, verifier_json.Signature)

		fmt.Println()
		fmt.Println(verifier_commit.Address,
			base64.StdEncoding.EncodeToString(
				verifier_commit.PublicKey))

		if index > 0 {
			if bytes.Compare(verifier_commit.PublicKey,
				verifier_commit_prev.PublicKey) < 0 {

				fmt.Println("    Order: invalid")
				reveal_valid = false
			} else {
				fmt.Println("    Order: correct")
			}
		}

		if signature_valid {
			fmt.Println("    Signature: valid")
		} else {
			fmt.Println("    Signature: invalid")
		}

		fmt.Printf("    Timestamp: %s\n", verifier_commit.Time)

		if err != nil {
			fmt.Println("    Reveal: invalid")
			reveal_valid = false
		} else {
			hashed_reveal := sha256.Sum256(verifier_json.Reveal)
			var valid_string string
			if bytes.Equal(hashed_reveal[:],
				verifier_commit.CommitValue) {
				valid_string = "(valid)"
			} else {
				valid_string = "(invalid)"
				reveal_valid = false
			}
			fmt.Printf("    Reveal: %s... %s\n",
				hex.EncodeToString(verifier_json.Reveal[0:10]),
				valid_string)
			fmt.Printf("    Commit: %s...\n",
				hex.EncodeToString(
					verifier_commit.CommitValue[0:10]))

			hash_context.Write(verifier_json.Reveal)
		}

		verifier_commit_prev = verifier_commit
	}

	var valid_string string
	if reveal_valid {
		valid_string = "(valid)"
	} else {
		valid_string = "(invalid)"
	}

	final_shared_random_value := hash_context.Sum(nil)
	fmt.Printf("\nShared random value: %s %s\n",
		hex.EncodeToString(final_shared_random_value), valid_string)

	fmt.Println("\nIdentity verifier sets:")
	for i := 1; i < len(args); i++ {
		verifiers, err := randomset.RandomSubset(
			final_shared_random_value,
			[]byte(args[i]),
			len(directory.Verifiers),
			*k)
		if err != nil {
			fmt.Printf("    %s: error (%s)\n", args[i], err.Error())
			continue
		} else {
			fmt.Printf("    %s: ", args[i])
			fmt.Println(verifiers)
		}
	}
}
<commit_msg>Added argument checking to read_directory.<commit_after>package main

import (
	"bytes"
	"crypto/sha256"
	"flag"
	"fmt"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"io/ioutil"
	"os"
	"time"

	"golang.org/x/crypto/ed25519"

	"requestor/randomset"
)

type Certificate struct {
	Host        []byte
	Certificate []byte
	Signature   []byte
}

type SignedDirectory struct {
	Directory []byte
	PublicKey ed25519.PublicKey
	Signature []byte
}

type DirectoryBody struct {
	Verifiers     []DirectoryEntry
	LastDirectory string
	Time          time.Time
	Validity      time.Duration
}

type DirectoryEntry struct {
	Commit          []byte
	Reveal          []byte
	Signature       []byte
}

type VerifierCommit struct {
	JSON        []byte
	Signature   []byte
}

type VerifierReveal struct {
	PublicKey   string
	RevealValue string
}

type Verifier struct {
	PublicKey   ed25519.PublicKey
	Address     string
	Time        time.Time
	CommitValue []byte
}

func main() {
	k := flag.Int("k", 1, "Verifiers needed per certificate")
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Fprintf(os.Stderr,
			"USAGE: read_directory [-k <verifiers>] <directory> "+
			"<identity> <identity> ...\n")
		os.Exit(1)
	}

	dirfile := args[0]

	fh, err := os.Open(dirfile)
	defer fh.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not open directory file: %s\n", err.Error())
		os.Exit(1)
	}

	data, err := ioutil.ReadAll(fh)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not read directory file.\n")
		os.Exit(1)
	}

	var directory_container SignedDirectory
	err = json.Unmarshal(data, &directory_container)
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not parse directory file: %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("Directory public key: %s\n",
		base64.StdEncoding.EncodeToString(
			directory_container.PublicKey))

	signature_valid := ed25519.Verify(directory_container.PublicKey,
		directory_container.Directory, directory_container.Signature)
	if signature_valid {
		fmt.Println("Signature: valid")
	} else {
		fmt.Println("Signature: invalid")
	}

	var directory DirectoryBody
	err = json.Unmarshal(directory_container.Directory, &directory)
	if err != nil {
		fmt.Fprintf(os.Stderr,
			"Could not parse directory body: %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("Valid %s from %s\n",
		directory.Validity.String(),
		directory.Time.Format("2006-01-02 15:04"))

	fmt.Printf("Found %d verifiers.\n", len(directory.Verifiers))

	var verifier_commit_prev Verifier

	hash_context := sha256.New()
	reveal_valid := true
	for index, verifier_json := range directory.Verifiers {
		var verifier_commit Verifier
		err = json.Unmarshal(verifier_json.Commit, &verifier_commit)
		if err != nil {
			fmt.Println("Verifier: invalid")
			reveal_valid = false
		}

		signature_valid := ed25519.Verify(verifier_commit.PublicKey,
			verifier_json.Commit, verifier_json.Signature)

		fmt.Println()
		fmt.Println(verifier_commit.Address,
			base64.StdEncoding.EncodeToString(
				verifier_commit.PublicKey))

		if index > 0 {
			if bytes.Compare(verifier_commit.PublicKey,
				verifier_commit_prev.PublicKey) < 0 {

				fmt.Println("    Order: invalid")
				reveal_valid = false
			} else {
				fmt.Println("    Order: correct")
			}
		}

		if signature_valid {
			fmt.Println("    Signature: valid")
		} else {
			fmt.Println("    Signature: invalid")
		}

		fmt.Printf("    Timestamp: %s\n", verifier_commit.Time)

		if err != nil {
			fmt.Println("    Reveal: invalid")
			reveal_valid = false
		} else {
			hashed_reveal := sha256.Sum256(verifier_json.Reveal)
			var valid_string string
			if bytes.Equal(hashed_reveal[:],
				verifier_commit.CommitValue) {
				valid_string = "(valid)"
			} else {
				valid_string = "(invalid)"
				reveal_valid = false
			}
			fmt.Printf("    Reveal: %s... %s\n",
				hex.EncodeToString(verifier_json.Reveal[0:10]),
				valid_string)
			fmt.Printf("    Commit: %s...\n",
				hex.EncodeToString(
					verifier_commit.CommitValue[0:10]))

			hash_context.Write(verifier_json.Reveal)
		}

		verifier_commit_prev = verifier_commit
	}

	var valid_string string
	if reveal_valid {
		valid_string = "(valid)"
	} else {
		valid_string = "(invalid)"
	}

	final_shared_random_value := hash_context.Sum(nil)
	fmt.Printf("\nShared random value: %s %s\n",
		hex.EncodeToString(final_shared_random_value), valid_string)

	if len(args) >= 2 {
		fmt.Println("\nIdentity verifier sets:")
	}

	for i := 1; i < len(args); i++ {
		verifiers, err := randomset.RandomSubset(
			final_shared_random_value,
			[]byte(args[i]),
			len(directory.Verifiers),
			*k)
		if err != nil {
			fmt.Printf("    %s: error (%s)\n", args[i], err.Error())
			continue
		} else {
			fmt.Printf("    %s: ", args[i])
			fmt.Println(verifiers)
		}
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import (
	"fmt"
	"github.com/emicklei/go-restful"
	"k8s.io/kube-openapi/pkg/common"
	"k8s.io/kube-openapi/pkg/util"
	"k8s.io/kube-openapi/pkg/validation/spec"
	"strings"
)

// CreateOpenAPIBuilderConfig hard-codes some values in the API builder
// config for testing.
func CreateOpenAPIBuilderConfig() *common.Config {
	return &common.Config{
		ProtocolList:   []string{"https"},
		IgnorePrefixes: []string{"/swaggerapi"},
		Info: &spec.Info{
			InfoProps: spec.InfoProps{
				Title:   "Integration Test",
				Version: "1.0",
			},
		},
		ResponseDefinitions: map[string]spec.Response{
			"NotFound": spec.Response{
				ResponseProps: spec.ResponseProps{
					Description: "Entity not found.",
				},
			},
		},
		CommonResponses: map[int]spec.Response{
			404: *spec.ResponseRef("#/responses/NotFound"),
		},
	}
}

// CreateWebServices hard-codes a simple WebService which only defines a GET and POST path
// for testing.
func CreateWebServices() []*restful.WebService {
	w := new(restful.WebService)
	addRoutes(w, buildRouteForType(w, "dummytype", "Foo")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Bar")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Baz")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Waldo")...)
	addRoutes(w, buildRouteForType(w, "listtype", "AtomicList")...)
	addRoutes(w, buildRouteForType(w, "listtype", "MapList")...)
	addRoutes(w, buildRouteForType(w, "listtype", "SetList")...)
	addRoutes(w, buildRouteForType(w, "uniontype", "TopLevelUnion")...)
	addRoutes(w, buildRouteForType(w, "uniontype", "InlinedUnion")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bal")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bak")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bac")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bah")...)
	addRoutes(w, buildRouteForType(w, "maptype", "GranularMap")...)
	addRoutes(w, buildRouteForType(w, "maptype", "AtomicMap")...)
	addRoutes(w, buildRouteForType(w, "structtype", "GranularStruct")...)
	addRoutes(w, buildRouteForType(w, "structtype", "AtomicStruct")...)
	addRoutes(w, buildRouteForType(w, "structtype", "DeclaredAtomicStruct")...)
	addRoutes(w, buildRouteForType(w, "defaults", "Defaulted")...)
	return []*restful.WebService{w}
}

func addRoutes(ws *restful.WebService, routes ...*restful.RouteBuilder) {
	for _, r := range routes {
		ws.Route(r)
	}
}

// Implements OpenAPICanonicalTypeNamer
var _ = util.OpenAPICanonicalTypeNamer(&typeNamer{})

type typeNamer struct {
	pkg  string
	name string
}

func (t *typeNamer) OpenAPICanonicalTypeName() string {
	return fmt.Sprintf("k8s.io/kube-openapi/test/integration/testdata/%s.%s", t.pkg, t.name)
}

func buildRouteForType(ws *restful.WebService, pkg, name string) []*restful.RouteBuilder {
	namer := typeNamer{
		pkg:  pkg,
		name: name,
	}

	var routes []*restful.RouteBuilder

	routes = append(routes, ws.GET(fmt.Sprintf("test/%s/%s", pkg, strings.ToLower(name))).
		Operation(fmt.Sprintf("get-%s.%s", pkg, name)).
		Produces("application/json").
		To(func(*restful.Request, *restful.Response) {}).
		Writes(&namer))

	routes = append(routes, ws.POST(fmt.Sprintf("test/%s", pkg)).
		Operation(fmt.Sprintf("create-%s.%s", pkg, name)).
		Produces("application/json").
		To(func(*restful.Request, *restful.Response) {}).
		Returns(201, "Created", &namer).
		Writes(&namer))

	if pkg == "dummytype" {
		statusErrType := typeNamer{
			pkg:  "dummytype",
			name: "StatusError",
		}

		for _, route := range routes {
			route.Returns(500, "Internal Service Error", &statusErrType)
		}
	}

	return routes
}
<commit_msg>Testutil cleanup<commit_after>/*
Copyright 2021 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import (
	"fmt"
	"strings"

	"github.com/emicklei/go-restful"
	"k8s.io/kube-openapi/pkg/common"
	"k8s.io/kube-openapi/pkg/util"
	"k8s.io/kube-openapi/pkg/validation/spec"
)

// CreateOpenAPIBuilderConfig hard-codes some values in the API builder
// config for testing.
func CreateOpenAPIBuilderConfig() *common.Config {
	return &common.Config{
		ProtocolList:   []string{"https"},
		IgnorePrefixes: []string{"/swaggerapi"},
		Info: &spec.Info{
			InfoProps: spec.InfoProps{
				Title:   "Integration Test",
				Version: "1.0",
			},
		},
		ResponseDefinitions: map[string]spec.Response{
			"NotFound": spec.Response{
				ResponseProps: spec.ResponseProps{
					Description: "Entity not found.",
				},
			},
		},
		CommonResponses: map[int]spec.Response{
			404: *spec.ResponseRef("#/responses/NotFound"),
		},
	}
}

// CreateWebServices hard-codes a simple WebService which only defines a GET and POST paths
// for testing.
func CreateWebServices() []*restful.WebService {
	w := new(restful.WebService)
	addRoutes(w, buildRouteForType(w, "dummytype", "Foo")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Bar")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Baz")...)
	addRoutes(w, buildRouteForType(w, "dummytype", "Waldo")...)
	addRoutes(w, buildRouteForType(w, "listtype", "AtomicList")...)
	addRoutes(w, buildRouteForType(w, "listtype", "MapList")...)
	addRoutes(w, buildRouteForType(w, "listtype", "SetList")...)
	addRoutes(w, buildRouteForType(w, "uniontype", "TopLevelUnion")...)
	addRoutes(w, buildRouteForType(w, "uniontype", "InlinedUnion")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bal")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bak")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bac")...)
	addRoutes(w, buildRouteForType(w, "custom", "Bah")...)
	addRoutes(w, buildRouteForType(w, "maptype", "GranularMap")...)
	addRoutes(w, buildRouteForType(w, "maptype", "AtomicMap")...)
	addRoutes(w, buildRouteForType(w, "structtype", "GranularStruct")...)
	addRoutes(w, buildRouteForType(w, "structtype", "AtomicStruct")...)
	addRoutes(w, buildRouteForType(w, "structtype", "DeclaredAtomicStruct")...)
	addRoutes(w, buildRouteForType(w, "defaults", "Defaulted")...)
	return []*restful.WebService{w}
}

func addRoutes(ws *restful.WebService, routes ...*restful.RouteBuilder) {
	for _, r := range routes {
		ws.Route(r)
	}
}

// Implements OpenAPICanonicalTypeNamer
var _ = util.OpenAPICanonicalTypeNamer(&typeNamer{})

type typeNamer struct {
	pkg  string
	name string
}

func (t *typeNamer) OpenAPICanonicalTypeName() string {
	return fmt.Sprintf("k8s.io/kube-openapi/test/integration/testdata/%s.%s", t.pkg, t.name)
}

func buildRouteForType(ws *restful.WebService, pkg, name string) []*restful.RouteBuilder {
	namer := typeNamer{
		pkg:  pkg,
		name: name,
	}

	routes := []*restful.RouteBuilder{
		ws.GET(fmt.Sprintf("test/%s/%s", pkg, strings.ToLower(name))).
			Operation(fmt.Sprintf("get-%s.%s", pkg, name)).
			Produces("application/json").
			To(func(*restful.Request, *restful.Response) {}).
			Writes(&namer),
		ws.POST(fmt.Sprintf("test/%s", pkg)).
			Operation(fmt.Sprintf("create-%s.%s", pkg, name)).
			Produces("application/json").
			To(func(*restful.Request, *restful.Response) {}).
			Returns(201, "Created", &namer).
			Writes(&namer),
	}

	if pkg == "dummytype" {
		statusErrType := typeNamer{
			pkg:  "dummytype",
			name: "StatusError",
		}

		for _, route := range routes {
			route.Returns(500, "Internal Service Error", &statusErrType)
		}
	}

	return routes
}
<|endoftext|>


<commit_before>package main

import "fmt"
import "os"

import "github.com/go-redis/redis"
import "github.com/joho/godotenv"

//import "github.com/stianeikeland/go-rpio"

func main() {
	godotenv.Load()

	redisChannelName := os.Getenv("UNSPEAK_CHANNEL_SPOKEN")
	redisAddress := os.Getenv("UNSPEAK_REDIS_ADDRESS")

	client := redis.NewClient(&redis.Options{
		Addr:     redisAddress,
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	defer client.Close()

	pubsub := client.Subscribe(redisChannelName)
	defer pubsub.Close()

	done := make(chan bool)
	msg := make(chan string)

	go pinWorker(msg)
	go redisWorker(pubsub, msg, done)

	//Wait until we are Done...
	<-done

	fmt.Println("Exiting")
}

func redisWorker(pubsub *redis.PubSub, msg chan<- string, done chan<- bool) {
	for {
		message, err := pubsub.ReceiveMessage()
		if err != nil {
			break
		}
		msg <- message.Payload
	}
	close(msg)
	done <- true
}

func pinWorker(msg <-chan string) {

	// err := rpio.Open(); err != nil {
	// 	fmt.Println(err)
	// 	os.Exit(1)
	// }

	// defer rpio.Close();

	//pin := rpio.Pin(10);
	for {
		message, more := <-msg
		if !more {
			break
		}
		//At this point we should increment the counter
		//pin.Toggle();
		fmt.Println(message)
	}
}
<commit_msg>Isoalted the Redis Client in the worker function Moved the redis client's initialization to the redisWorker function for better isolation<commit_after>package main

import "fmt"
import "os"

import "github.com/go-redis/redis"
import "github.com/joho/godotenv"

//import "github.com/stianeikeland/go-rpio"

func main() {
	godotenv.Load()

	redisChannelName := os.Getenv("UNSPEAK_CHANNEL_SPOKEN")
	redisAddress := os.Getenv("UNSPEAK_REDIS_ADDRESS")

	done := make(chan bool)
	msg := make(chan string)

	go pinWorker(msg)
	go redisWorker(redisAddress, redisChannelName, msg, done)

	//Wait until we are Done...
	<-done

	fmt.Println("Exiting")
}

func redisWorker(redisAddress string, redisChannelName string, msg chan<- string, done chan<- bool) {
	client := redis.NewClient(&redis.Options{
		Addr:     redisAddress,
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	defer client.Close()

	pubsub := client.Subscribe(redisChannelName)
	defer pubsub.Close()

	for {
		message, err := pubsub.ReceiveMessage()
		if err != nil {
			break
		}
		msg <- message.Payload
	}
	close(msg)
	done <- true
}

func pinWorker(msg <-chan string) {

	// err := rpio.Open(); err != nil {
	// 	fmt.Println(err)
	// 	os.Exit(1)
	// }

	// defer rpio.Close();

	//pin := rpio.Pin(10);
	for {
		message, more := <-msg
		if !more {
			break
		}
		//At this point we should increment the counter
		//pin.Toggle();
		fmt.Println(message)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"

	"github.com/scrapli/scrapligo/driver/base"
	"github.com/scrapli/scrapligo/driver/core"
)

// const commandsFile = "commandsfile"

func main() {
	arg := flag.String("file", "examples/simple/commandsfile", "argument from user")
	flag.Parse()

	d, err := core.NewCoreDriver(
		"ios-xe-mgmt.cisco.com",
		"cisco_iosxe",
		base.WithPort(8181),
		base.WithAuthStrictKey(false),
		base.WithAuthUsername("developer"),
		base.WithAuthPassword("C1sco12345"),
	)

	if err != nil {
		fmt.Printf("failed to create driver; error: %+v\n", err)
		return
	}

	err = d.Open()
	if err != nil {
		fmt.Printf("failed to open driver; error: %+v\n", err)
		return
	}
	defer d.Close()

	// fetch the prompt
	prompt, err := d.GetPrompt()
	if err != nil {
		fmt.Printf("failed to get prompt; error: %+v\n", err)
	} else {
		fmt.Printf("found prompt: %s\n\n\n", prompt)
	}

	// send some commands from a file
	mr, err := d.SendCommandsFromFile(*arg)
	if err != nil {
		fmt.Printf("failed to send commands from file; error: %+v\n", err)
		return
	}
	for _, r := range mr.Responses {
		fmt.Printf("sent command '%s', output received:\n %s\n\n\n", r.ChannelInput, r.Result)
	}

	// send some configs
	configs := []string{
		"interface loopback0",
		"interface loopback0 description tacocat",
		"no interface loopback0",
	}

	_, err = d.SendConfigs(configs)
	if err != nil {
		fmt.Printf("failed to send configs; error: %+v\n", err)
		return
	}
}
<commit_msg>fix example file link<commit_after>package main

import (
	"flag"
	"fmt"

	"github.com/scrapli/scrapligo/driver/base"
	"github.com/scrapli/scrapligo/driver/core"
)

// const commandsFile = "commandsfile"

func main() {
	arg := flag.String("file", "examples/network_driver/simple/commandsfile", "argument from user")
	flag.Parse()

	d, err := core.NewCoreDriver(
		"ios-xe-mgmt.cisco.com",
		"cisco_iosxe",
		base.WithPort(8181),
		base.WithAuthStrictKey(false),
		base.WithAuthUsername("developer"),
		base.WithAuthPassword("C1sco12345"),
	)

	if err != nil {
		fmt.Printf("failed to create driver; error: %+v\n", err)
		return
	}

	err = d.Open()
	if err != nil {
		fmt.Printf("failed to open driver; error: %+v\n", err)
		return
	}
	defer d.Close()

	// fetch the prompt
	prompt, err := d.GetPrompt()
	if err != nil {
		fmt.Printf("failed to get prompt; error: %+v\n", err)
	} else {
		fmt.Printf("found prompt: %s\n\n\n", prompt)
	}

	// send some commands from a file
	mr, err := d.SendCommandsFromFile(*arg)
	if err != nil {
		fmt.Printf("failed to send commands from file; error: %+v\n", err)
		return
	}
	for _, r := range mr.Responses {
		fmt.Printf("sent command '%s', output received:\n %s\n\n\n", r.ChannelInput, r.Result)
	}

	// send some configs
	configs := []string{
		"interface loopback0",
		"interface loopback0 description tacocat",
		"no interface loopback0",
	}

	_, err = d.SendConfigs(configs)
	if err != nil {
		fmt.Printf("failed to send configs; error: %+v\n", err)
		return
	}
}
<|endoftext|>