<commit_before>//wsserver.go
package ws

import (
	"encoding/json"
	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/jayaramsankara/gotell/apns"
	"gopkg.in/redis.v5"
	"log"
	"net/http"
	"os"
	"time"
	"github.com/satori/go.uuid"
	"strconv"
)

const (
	// Time allowed to write a message to the peer.
	writeWait = 10 * time.Second

	// Time allowed to read the next pong message from the peer.
	pongWait = 30 * time.Second

	// Send pings to peer with this period. Must be less than pongWait.
	pingPeriod = (pongWait * 9) / 10

	// Maximum message size allowed from peer.
	maxMessageSize = 512

	// Max NUmber of Redis connections maintained for publishing events
	// This is same as the Go Channel buffer size for sending events for redis
	maxRedisConn = 50
)

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin:     func(r *http.Request) bool { return true },
}

var clientConnections = map[string][]*wsconnection{}

var redisSender chan *NotifyData

var receiver *redis.PubSub

var logs = log.New(os.Stdout, "INFO", log.LstdFlags)

var Logs = logs

type wsconnection struct {
	// The websocket connection.
	ws       *websocket.Conn
	clientId string
    id  string
	//The redis pubsubs channel
	// Buffered channel of outbound messages.
	//receive *redis.PubSub
	send   chan ([]byte)
	active bool
}

type redisclients struct {
	receiver *redis.PubSub
	sender   *redis.Client
}

type WsMessage struct {
	Message string `json:"message"`
}

type NotifyData struct {
	ClientId string
	Message  string
}

type NotifyResponse struct {
	Status bool `json:"status"`
	ClientId string `json:"clientId"`

}

func (conn *wsconnection) Close() {
	conn.write(websocket.CloseMessage, []byte{})
	conn.ws.Close()
}

func (conn *wsconnection) String() (string) {
	return conn.clientId+" : "+conn.id
}

func (conn *wsconnection) sendMessages() {
	logs.Println("Initing sendMessages for client  " + conn.String())
	clientId := conn.clientId
	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		logs.Println("Exiting sendMessages for client  : " + conn.String())
		conn.active = false
		logs.Println("Closing websocket connection for ", conn.String())
		conn.Close()

		currentConnections := clientConnections[clientId]
		if len(currentConnections) == 1 {
			logs.Println("Removing subscription to redis channel for ", conn.String())
			receiver.Unsubscribe(conn.clientId)
		}
		logs.Println("Removing the connection mapped to " + conn.String())
		newConnections := currentConnections[:0]
		for _, ec := range currentConnections {
			if ec.id != conn.id {
				newConnections = append(newConnections, ec)
			}
		}
		clientConnections[clientId]=newConnections
		logs.Println("Exiting sendMessages. Current # conenctions for client " + clientId+" is "+strconv.Itoa(len(clientConnections[clientId])))
	}()

	for {
		select {
		case message, ok := <-conn.send:
			if !ok {
				log.Println("No more messages to be sent to WS connection from channel for  ", conn.String())
				return
			}
			logs.Println("Sending WS message for client  " + conn.String())
			if err := conn.write(websocket.TextMessage, message); err != nil {
				log.Println("Error while sending WS message  ", conn.String(), message, err)
				return
			}
		case <-ticker.C:
			logs.Println("Sending Ping WS message for client  " + conn.String())
			if err := conn.write(websocket.PingMessage, []byte{}); err != nil {
				log.Println("Error while sending WS ping message  ", conn.String(), err)
				return
			}
		}
	}

}

func (conn *wsconnection) receiveMessages() {
	logs.Println("Initing receiveMessages for client  " + conn.String())
	go func() {
		for {
			if _, _, err := conn.ws.NextReader(); err != nil {
				log.Println("Error while receiving WS pong message  ", conn.String(), err)
				conn.active = false
				conn.Close()
				break
			}
		}
	}()

	conn.ws.SetReadLimit(maxMessageSize)
	conn.ws.SetReadDeadline(time.Now().Add(pongWait))
	conn.ws.SetPongHandler(func(string) error {
		logs.Println("PongHandler for client  " + conn.String())
		conn.ws.SetReadDeadline(time.Now().Add(pongWait))
		return nil
	})
}

// write writes a message with the given message type and payload.
func (conn *wsconnection) write(messageType int, payload []byte) error {
	conn.ws.SetWriteDeadline(time.Now().Add(writeWait))
	return conn.ws.WriteMessage(messageType, payload)
}

func InitPubSub(redisConf *redis.Options) error {
	redisSender = make(chan *NotifyData, redisConf.PoolSize)
	receiver, _ = redis.NewClient(redisConf).Subscribe()

	var publisher = redis.NewClient(redisConf)

	logs.Println("Initialized redis clients for pub and sub.")

	//Init Redis receiver
	go func() {
		for {
			var msgi, err = receiver.Receive()
			if err != nil {
				log.Println("Error while receive message from redis pubsub receiver", err)
				// handle failure.. reinit redis pub sub
				log.Println("Reinitializing Redis PubSub.. and exiting the receive handler routine")
				close(redisSender)
				receiver.Close()
				publisher.Close()
				InitPubSub(redisConf)
				return

			} else {

				switch msg := msgi.(type) {
				case *redis.Subscription:
					logs.Println("Received messagefrom Redis ", msg.Kind, msg.Channel)
				case *redis.Message:
					logs.Println("Received message from Redis ", msg.Payload, msg.Channel)
					go func(clientId string, message string) {
						logs.Println("Attempting to send the message to WS  connections for client ", message, clientId)
						connections := clientConnections[clientId]
						for cnt := range connections {
							logs.Println("Sending the message to WS  client  ", message, connections[cnt].String())
							connections[cnt].send <- []byte(message)
						}

					}(msg.Channel, msg.Payload)

				case *redis.Pong:
					logs.Println("Pong message from Redis : ", msg)
				default:
					log.Printf("Error unknown message from Redis : %#v", msgi)
				}

			}

		}

	}()

	go func(msgsForRedis chan *NotifyData) {
		for {
			data, ok := <-msgsForRedis
			if !ok {
				log.Println("RedisSender channel seems to be closed. Exiting the routine")
				return
			}
			logs.Println("Received notification data from redisSender. ", data.ClientId)
			go func() {
				logs.Println("Publishing message to the Redis ", data.Message, data.ClientId)
				err := publisher.Publish(data.ClientId, data.Message).Err()
				if err != nil {
					log.Println("Error in publishing event to redis", err)
				}
			}()

		}

	}(redisSender)
	return nil
}

func ServeApns(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	deviceToken := vars["devicetoken"]

	//extract body

	decoder := json.NewDecoder(r.Body)
	var data apns.ApnsMessage
	err := decoder.Decode(&data)

	if err != nil {
		log.Println("Error while extracting the apns message.", err)
		w.WriteHeader(http.StatusInternalServerError)

	} else {
		// send data to conn
		logs.Println("Handling apns:  The extracted message is : ", data.Message, data.Badge, data.Sound)
		apns.Notify(&data, deviceToken)
		w.WriteHeader(http.StatusOK)
	}

}

//serveNotify receives the API, parses the body and sends the message to the corresponding
// websocket. Returns error if no websocket conn exists for a client id or send fails
func ServeNotify(w http.ResponseWriter, r *http.Request) {

	vars := mux.Vars(r)
	clientId := vars["clientid"]

	//extract body

	decoder := json.NewDecoder(r.Body)
	var data WsMessage
	err := decoder.Decode(&data)

	if err != nil {
		log.Println("Error while extracting the message.", err)
		w.WriteHeader(http.StatusInternalServerError)

	} else {
		// send data to conn
		logs.Println("Handling notify:  The extracted message is : ", data.Message, clientId)
		notifyData := &NotifyData{
			ClientId: clientId,
			Message:  data.Message,
		}
		logs.Println("Handling notify:  Sending notification data to redisSender ", clientId)
		redisSender <- notifyData
		//Check whether clientId is connected.
		currentConnections := clientConnections[clientId]

		notifyResponse := NotifyResponse{
			Status : (len(currentConnections) > 1),
		    ClientId: clientId,
		}
		w.Header().Set("Content-Type", "application/json")
		err = json.NewEncoder(w).Encode(notifyResponse)
		if(err != nil) {
			log.Println("Error writing the response.", err)
			w.WriteHeader(http.StatusInternalServerError)
		}

	}

}

// serverWs handles websocket requests from the peer.
func ServeWs(w http.ResponseWriter, r *http.Request) {

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println("Error in accepting websocket connection", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	//new websocket connection
	vars := mux.Vars(r)
	clientId := vars["clientid"]
	logs.Println("Creating new wsconnection for client : " + clientId)

	currentConnections := clientConnections[clientId]
	//Subscribe for a client only for the first connection.
	if len(currentConnections) == 0 {
		// Initialize redis client as subscriber

		err = receiver.Subscribe(clientId)
		if err != nil {
			errMsg := "Failed to subscribe to message channel for " + clientId
			log.Println(errMsg, err)
			ws.WriteMessage(websocket.CloseInternalServerErr, []byte(errMsg))
			ws.Close()
			return
		}
	}

	conn := &wsconnection{active: true, clientId: clientId, id: newUUID() , ws: ws, send: make(chan []byte, 256)}
	logs.Println("Adding clientId-WsConn mapping for client  " + conn.String())

	modifiedConnections := append(currentConnections, conn)
	logs.Println("# connections for  client after appending the new one " + strconv.Itoa(len(modifiedConnections)))

	clientConnections[clientId] = modifiedConnections
	logs.Println("#  updated connections for  client  " + strconv.Itoa(len(clientConnections[clientId])))
	go conn.sendMessages()
	conn.receiveMessages()
}

func newUUID() (string) {

	u1 := uuid.NewV4()
	return u1.String()
}
<commit_msg>Fixed the bug - off by one >1 should be >0<commit_after>//wsserver.go
package ws

import (
	"encoding/json"
	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/jayaramsankara/gotell/apns"
	"gopkg.in/redis.v5"
	"log"
	"net/http"
	"os"
	"time"
	"github.com/satori/go.uuid"
	"strconv"
)

const (
	// Time allowed to write a message to the peer.
	writeWait = 10 * time.Second

	// Time allowed to read the next pong message from the peer.
	pongWait = 30 * time.Second

	// Send pings to peer with this period. Must be less than pongWait.
	pingPeriod = (pongWait * 9) / 10

	// Maximum message size allowed from peer.
	maxMessageSize = 512

	// Max NUmber of Redis connections maintained for publishing events
	// This is same as the Go Channel buffer size for sending events for redis
	maxRedisConn = 50
)

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin:     func(r *http.Request) bool { return true },
}

var clientConnections = map[string][]*wsconnection{}

var redisSender chan *NotifyData

var receiver *redis.PubSub

var logs = log.New(os.Stdout, "INFO", log.LstdFlags)

var Logs = logs

type wsconnection struct {
	// The websocket connection.
	ws       *websocket.Conn
	clientId string
    id  string
	//The redis pubsubs channel
	// Buffered channel of outbound messages.
	//receive *redis.PubSub
	send   chan ([]byte)
	active bool
}

type redisclients struct {
	receiver *redis.PubSub
	sender   *redis.Client
}

type WsMessage struct {
	Message string `json:"message"`
}

type NotifyData struct {
	ClientId string
	Message  string
}

type NotifyResponse struct {
	Status bool `json:"status"`
	ClientId string `json:"clientId"`

}

func (conn *wsconnection) Close() {
	conn.write(websocket.CloseMessage, []byte{})
	conn.ws.Close()
}

func (conn *wsconnection) String() (string) {
	return conn.clientId+" : "+conn.id
}

func (conn *wsconnection) sendMessages() {
	logs.Println("Initing sendMessages for client  " + conn.String())
	clientId := conn.clientId
	ticker := time.NewTicker(pingPeriod)
	defer func() {
		ticker.Stop()
		logs.Println("Exiting sendMessages for client  : " + conn.String())
		conn.active = false
		logs.Println("Closing websocket connection for ", conn.String())
		conn.Close()

		currentConnections := clientConnections[clientId]
		if len(currentConnections) == 1 {
			logs.Println("Removing subscription to redis channel for ", conn.String())
			receiver.Unsubscribe(conn.clientId)
		}
		logs.Println("Removing the connection mapped to " + conn.String())
		newConnections := currentConnections[:0]
		for _, ec := range currentConnections {
			if ec.id != conn.id {
				newConnections = append(newConnections, ec)
			}
		}
		clientConnections[clientId]=newConnections
		logs.Println("Exiting sendMessages. Current # conenctions for client " + clientId+" is "+strconv.Itoa(len(clientConnections[clientId])))
	}()

	for {
		select {
		case message, ok := <-conn.send:
			if !ok {
				log.Println("No more messages to be sent to WS connection from channel for  ", conn.String())
				return
			}
			logs.Println("Sending WS message for client  " + conn.String())
			if err := conn.write(websocket.TextMessage, message); err != nil {
				log.Println("Error while sending WS message  ", conn.String(), message, err)
				return
			}
		case <-ticker.C:
			logs.Println("Sending Ping WS message for client  " + conn.String())
			if err := conn.write(websocket.PingMessage, []byte{}); err != nil {
				log.Println("Error while sending WS ping message  ", conn.String(), err)
				return
			}
		}
	}

}

func (conn *wsconnection) receiveMessages() {
	logs.Println("Initing receiveMessages for client  " + conn.String())
	go func() {
		for {
			if _, _, err := conn.ws.NextReader(); err != nil {
				log.Println("Error while receiving WS pong message  ", conn.String(), err)
				conn.active = false
				conn.Close()
				break
			}
		}
	}()

	conn.ws.SetReadLimit(maxMessageSize)
	conn.ws.SetReadDeadline(time.Now().Add(pongWait))
	conn.ws.SetPongHandler(func(string) error {
		logs.Println("PongHandler for client  " + conn.String())
		conn.ws.SetReadDeadline(time.Now().Add(pongWait))
		return nil
	})
}

// write writes a message with the given message type and payload.
func (conn *wsconnection) write(messageType int, payload []byte) error {
	conn.ws.SetWriteDeadline(time.Now().Add(writeWait))
	return conn.ws.WriteMessage(messageType, payload)
}

func InitPubSub(redisConf *redis.Options) error {
	redisSender = make(chan *NotifyData, redisConf.PoolSize)
	receiver, _ = redis.NewClient(redisConf).Subscribe()

	var publisher = redis.NewClient(redisConf)

	logs.Println("Initialized redis clients for pub and sub.")

	//Init Redis receiver
	go func() {
		for {
			var msgi, err = receiver.Receive()
			if err != nil {
				log.Println("Error while receive message from redis pubsub receiver", err)
				// handle failure.. reinit redis pub sub
				log.Println("Reinitializing Redis PubSub.. and exiting the receive handler routine")
				close(redisSender)
				receiver.Close()
				publisher.Close()
				InitPubSub(redisConf)
				return

			} else {

				switch msg := msgi.(type) {
				case *redis.Subscription:
					logs.Println("Received messagefrom Redis ", msg.Kind, msg.Channel)
				case *redis.Message:
					logs.Println("Received message from Redis ", msg.Payload, msg.Channel)
					go func(clientId string, message string) {
						logs.Println("Attempting to send the message to WS  connections for client ", message, clientId)
						connections := clientConnections[clientId]
						for cnt := range connections {
							logs.Println("Sending the message to WS  client  ", message, connections[cnt].String())
							connections[cnt].send <- []byte(message)
						}

					}(msg.Channel, msg.Payload)

				case *redis.Pong:
					logs.Println("Pong message from Redis : ", msg)
				default:
					log.Printf("Error unknown message from Redis : %#v", msgi)
				}

			}

		}

	}()

	go func(msgsForRedis chan *NotifyData) {
		for {
			data, ok := <-msgsForRedis
			if !ok {
				log.Println("RedisSender channel seems to be closed. Exiting the routine")
				return
			}
			logs.Println("Received notification data from redisSender. ", data.ClientId)
			go func() {
				logs.Println("Publishing message to the Redis ", data.Message, data.ClientId)
				err := publisher.Publish(data.ClientId, data.Message).Err()
				if err != nil {
					log.Println("Error in publishing event to redis", err)
				}
			}()

		}

	}(redisSender)
	return nil
}

func ServeApns(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	deviceToken := vars["devicetoken"]

	//extract body

	decoder := json.NewDecoder(r.Body)
	var data apns.ApnsMessage
	err := decoder.Decode(&data)

	if err != nil {
		log.Println("Error while extracting the apns message.", err)
		w.WriteHeader(http.StatusInternalServerError)

	} else {
		// send data to conn
		logs.Println("Handling apns:  The extracted message is : ", data.Message, data.Badge, data.Sound)
		apns.Notify(&data, deviceToken)
		w.WriteHeader(http.StatusOK)
	}

}

//serveNotify receives the API, parses the body and sends the message to the corresponding
// websocket. Returns error if no websocket conn exists for a client id or send fails
func ServeNotify(w http.ResponseWriter, r *http.Request) {

	vars := mux.Vars(r)
	clientId := vars["clientid"]

	//extract body

	decoder := json.NewDecoder(r.Body)
	var data WsMessage
	err := decoder.Decode(&data)

	if err != nil {
		log.Println("Error while extracting the message.", err)
		w.WriteHeader(http.StatusInternalServerError)

	} else {
		// send data to conn
		logs.Println("Handling notify:  The extracted message is : ", data.Message, clientId)
		notifyData := &NotifyData{
			ClientId: clientId,
			Message:  data.Message,
		}
		logs.Println("Handling notify:  Sending notification data to redisSender ", clientId)
		redisSender <- notifyData
		//Check whether clientId is connected.
		currentConnections := clientConnections[clientId]

		notifyResponse := NotifyResponse{
			Status : (len(currentConnections) > 0 ),
		    ClientId: clientId,
		}
		w.Header().Set("Content-Type", "application/json")
		err = json.NewEncoder(w).Encode(notifyResponse)
		if(err != nil) {
			log.Println("Error writing the response.", err)
			w.WriteHeader(http.StatusInternalServerError)
		}

	}

}

// serverWs handles websocket requests from the peer.
func ServeWs(w http.ResponseWriter, r *http.Request) {

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Println("Error in accepting websocket connection", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	//new websocket connection
	vars := mux.Vars(r)
	clientId := vars["clientid"]
	logs.Println("Creating new wsconnection for client : " + clientId)

	currentConnections := clientConnections[clientId]
	//Subscribe for a client only for the first connection.
	if len(currentConnections) == 0 {
		// Initialize redis client as subscriber

		err = receiver.Subscribe(clientId)
		if err != nil {
			errMsg := "Failed to subscribe to message channel for " + clientId
			log.Println(errMsg, err)
			ws.WriteMessage(websocket.CloseInternalServerErr, []byte(errMsg))
			ws.Close()
			return
		}
	}

	conn := &wsconnection{active: true, clientId: clientId, id: newUUID() , ws: ws, send: make(chan []byte, 256)}
	logs.Println("Adding clientId-WsConn mapping for client  " + conn.String())

	modifiedConnections := append(currentConnections, conn)
	logs.Println("# connections for  client after appending the new one " + strconv.Itoa(len(modifiedConnections)))

	clientConnections[clientId] = modifiedConnections
	logs.Println("#  updated connections for  client  " + strconv.Itoa(len(clientConnections[clientId])))
	go conn.sendMessages()
	conn.receiveMessages()
}

func newUUID() (string) {

	u1 := uuid.NewV4()
	return u1.String()
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Knative 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 networking

import (
	"context"

	"knative.dev/pkg/apis"
)

// This files contains the versionless types and enums that are strongly
// unlikely to change from version to version.

// ProtocolType is an enumeration of the supported application-layer protocols
// See also: https://knative.dev/serving/blob/master/docs/runtime-contract.md#protocols-and-ports
type ProtocolType string

const (
	// ProtocolHTTP1 maps to HTTP/1.1.
	ProtocolHTTP1 ProtocolType = "http1"
	// ProtocolH2C maps to HTTP/2 with Prior Knowledge.
	ProtocolH2C ProtocolType = "h2c"
)

// Validate validates that ProtocolType has a correct enum value.
func (p ProtocolType) Validate(context.Context) *apis.FieldError {
	switch p {
	case ProtocolH2C, ProtocolHTTP1:
		return nil
	}
	return apis.ErrInvalidValue(p, apis.CurrentField)
}
<commit_msg>Remove and cleanup the promised todos (#4835)<commit_after>/*
Copyright 2019 The Knative 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 networking

import (
	"context"

	"knative.dev/pkg/apis"
)

// This files contains the versionless types and enums that are strongly
// unlikely to change from version to version.

// ProtocolType is an enumeration of the supported application-layer protocols
// See also: https://knative.dev/serving/blob/master/docs/runtime-contract.md#protocols-and-ports
type ProtocolType string

const (
	// ProtocolHTTP1 maps to HTTP/1.1.
	ProtocolHTTP1 ProtocolType = "http1"
	// ProtocolH2C maps to HTTP/2 with Prior Knowledge.
	ProtocolH2C ProtocolType = "h2c"
)

// Validate validates that ProtocolType has a correct enum value.
func (p ProtocolType) Validate(context.Context) *apis.FieldError {
	switch p {
	case ProtocolH2C, ProtocolHTTP1:
		return nil
	case ProtocolType(""):
		return apis.ErrMissingField(apis.CurrentField)
	}
	return apis.ErrInvalidValue(p, apis.CurrentField)
}
<|endoftext|>


<commit_before>/*
Copyright 2014 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 authenticator

import (
	"time"

	"github.com/go-openapi/spec"

	"k8s.io/kubernetes/pkg/auth/authenticator"
	"k8s.io/kubernetes/pkg/auth/authenticator/bearertoken"
	"k8s.io/kubernetes/pkg/auth/group"
	"k8s.io/kubernetes/pkg/auth/user"
	"k8s.io/kubernetes/pkg/serviceaccount"
	certutil "k8s.io/kubernetes/pkg/util/cert"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/password/keystone"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/password/passwordfile"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/anonymous"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/basicauth"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/headerrequest"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/union"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/x509"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/anytoken"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/oidc"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/tokenfile"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/webhook"
)

type RequestHeaderConfig struct {
	// UsernameHeaders are the headers to check (in order, case-insensitively) for an identity. The first header with a value wins.
	UsernameHeaders []string
	// ClientCA points to CA bundle file which is used verify the identity of the front proxy
	ClientCA string
	// AllowedClientNames is a list of common names that may be presented by the authenticating front proxy.  Empty means: accept any.
	AllowedClientNames []string
}

type AuthenticatorConfig struct {
	Anonymous                   bool
	AnyToken                    bool
	BasicAuthFile               string
	ClientCAFile                string
	TokenAuthFile               string
	OIDCIssuerURL               string
	OIDCClientID                string
	OIDCCAFile                  string
	OIDCUsernameClaim           string
	OIDCGroupsClaim             string
	ServiceAccountKeyFiles      []string
	ServiceAccountLookup        bool
	ServiceAccountTokenGetter   serviceaccount.ServiceAccountTokenGetter
	KeystoneURL                 string
	KeystoneCAFile              string
	WebhookTokenAuthnConfigFile string
	WebhookTokenAuthnCacheTTL   time.Duration

	RequestHeaderConfig *RequestHeaderConfig
}

// New returns an authenticator.Request or an error that supports the standard
// Kubernetes authentication mechanisms.
func New(config AuthenticatorConfig) (authenticator.Request, *spec.SecurityDefinitions, error) {
	var authenticators []authenticator.Request
	securityDefinitions := spec.SecurityDefinitions{}
	hasBasicAuth := false
	hasTokenAuth := false

	// front-proxy, BasicAuth methods, local first, then remote
	// Add the front proxy authenticator if requested
	if config.RequestHeaderConfig != nil {
		requestHeaderAuthenticator, err := headerrequest.NewSecure(
			config.RequestHeaderConfig.ClientCA,
			config.RequestHeaderConfig.AllowedClientNames,
			config.RequestHeaderConfig.UsernameHeaders,
		)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, requestHeaderAuthenticator)
	}

	if len(config.BasicAuthFile) > 0 {
		basicAuth, err := newAuthenticatorFromBasicAuthFile(config.BasicAuthFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, basicAuth)
		hasBasicAuth = true
	}
	if len(config.KeystoneURL) > 0 {
		keystoneAuth, err := newAuthenticatorFromKeystoneURL(config.KeystoneURL, config.KeystoneCAFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, keystoneAuth)
		hasBasicAuth = true
	}

	// X509 methods
	if len(config.ClientCAFile) > 0 {
		certAuth, err := newAuthenticatorFromClientCAFile(config.ClientCAFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, certAuth)
	}

	// Bearer token methods, local first, then remote
	if len(config.TokenAuthFile) > 0 {
		tokenAuth, err := newAuthenticatorFromTokenFile(config.TokenAuthFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, tokenAuth)
		hasTokenAuth = true
	}
	if len(config.ServiceAccountKeyFiles) > 0 {
		serviceAccountAuth, err := newServiceAccountAuthenticator(config.ServiceAccountKeyFiles, config.ServiceAccountLookup, config.ServiceAccountTokenGetter)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, serviceAccountAuth)
		hasTokenAuth = true
	}
	// NOTE(ericchiang): Keep the OpenID Connect after Service Accounts.
	//
	// Because both plugins verify JWTs whichever comes first in the union experiences
	// cache misses for all requests using the other. While the service account plugin
	// simply returns an error, the OpenID Connect plugin may query the provider to
	// update the keys, causing performance hits.
	if len(config.OIDCIssuerURL) > 0 && len(config.OIDCClientID) > 0 {
		oidcAuth, err := newAuthenticatorFromOIDCIssuerURL(config.OIDCIssuerURL, config.OIDCClientID, config.OIDCCAFile, config.OIDCUsernameClaim, config.OIDCGroupsClaim)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, oidcAuth)
		hasTokenAuth = true
	}
	if len(config.WebhookTokenAuthnConfigFile) > 0 {
		webhookTokenAuth, err := newWebhookTokenAuthenticator(config.WebhookTokenAuthnConfigFile, config.WebhookTokenAuthnCacheTTL)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, webhookTokenAuth)
		hasTokenAuth = true
	}

	// always add anytoken last, so that every other token authenticator gets to try first
	if config.AnyToken {
		authenticators = append(authenticators, bearertoken.New(anytoken.AnyTokenAuthenticator{}))
		hasTokenAuth = true
	}

	if hasBasicAuth {
		securityDefinitions["HTTPBasic"] = &spec.SecurityScheme{
			SecuritySchemeProps: spec.SecuritySchemeProps{
				Type:        "basic",
				Description: "HTTP Basic authentication",
			},
		}
	}

	if hasTokenAuth {
		securityDefinitions["BearerToken"] = &spec.SecurityScheme{
			SecuritySchemeProps: spec.SecuritySchemeProps{
				Type:        "apiKey",
				Name:        "authorization",
				In:          "header",
				Description: "Bearer Token authentication",
			},
		}
	}

	if len(authenticators) == 0 {
		if config.Anonymous {
			return anonymous.NewAuthenticator(), &securityDefinitions, nil
		}
	}

	switch len(authenticators) {
	case 0:
		return nil, &securityDefinitions, nil
	}

	authenticator := union.New(authenticators...)

	authenticator = group.NewGroupAdder(authenticator, []string{user.AllAuthenticated})

	if config.Anonymous {
		// If the authenticator chain returns an error, return an error (don't consider a bad bearer token anonymous).
		authenticator = union.NewFailOnError(authenticator, anonymous.NewAuthenticator())
	}

	return authenticator, &securityDefinitions, nil
}

// IsValidServiceAccountKeyFile returns true if a valid public RSA key can be read from the given file
func IsValidServiceAccountKeyFile(file string) bool {
	_, err := serviceaccount.ReadPublicKeys(file)
	return err == nil
}

// newAuthenticatorFromBasicAuthFile returns an authenticator.Request or an error
func newAuthenticatorFromBasicAuthFile(basicAuthFile string) (authenticator.Request, error) {
	basicAuthenticator, err := passwordfile.NewCSV(basicAuthFile)
	if err != nil {
		return nil, err
	}

	return basicauth.New(basicAuthenticator), nil
}

// newAuthenticatorFromTokenFile returns an authenticator.Request or an error
func newAuthenticatorFromTokenFile(tokenAuthFile string) (authenticator.Request, error) {
	tokenAuthenticator, err := tokenfile.NewCSV(tokenAuthFile)
	if err != nil {
		return nil, err
	}

	return bearertoken.New(tokenAuthenticator), nil
}

// newAuthenticatorFromToken returns an authenticator.Request or an error
func NewAuthenticatorFromTokens(tokens map[string]*user.DefaultInfo) authenticator.Request {
	return bearertoken.New(tokenfile.New(tokens))
}

// newAuthenticatorFromOIDCIssuerURL returns an authenticator.Request or an error.
func newAuthenticatorFromOIDCIssuerURL(issuerURL, clientID, caFile, usernameClaim, groupsClaim string) (authenticator.Request, error) {
	tokenAuthenticator, err := oidc.New(oidc.OIDCOptions{
		IssuerURL:     issuerURL,
		ClientID:      clientID,
		CAFile:        caFile,
		UsernameClaim: usernameClaim,
		GroupsClaim:   groupsClaim,
	})
	if err != nil {
		return nil, err
	}

	return bearertoken.New(tokenAuthenticator), nil
}

// newServiceAccountAuthenticator returns an authenticator.Request or an error
func newServiceAccountAuthenticator(keyfiles []string, lookup bool, serviceAccountGetter serviceaccount.ServiceAccountTokenGetter) (authenticator.Request, error) {
	allPublicKeys := []interface{}{}
	for _, keyfile := range keyfiles {
		publicKeys, err := serviceaccount.ReadPublicKeys(keyfile)
		if err != nil {
			return nil, err
		}
		allPublicKeys = append(allPublicKeys, publicKeys...)
	}

	tokenAuthenticator := serviceaccount.JWTTokenAuthenticator(allPublicKeys, lookup, serviceAccountGetter)
	return bearertoken.New(tokenAuthenticator), nil
}

// newAuthenticatorFromClientCAFile returns an authenticator.Request or an error
func newAuthenticatorFromClientCAFile(clientCAFile string) (authenticator.Request, error) {
	roots, err := certutil.NewPool(clientCAFile)
	if err != nil {
		return nil, err
	}

	opts := x509.DefaultVerifyOptions()
	opts.Roots = roots

	return x509.New(opts, x509.CommonNameUserConversion), nil
}

// newAuthenticatorFromTokenFile returns an authenticator.Request or an error
func newAuthenticatorFromKeystoneURL(keystoneURL string, keystoneCAFile string) (authenticator.Request, error) {
	keystoneAuthenticator, err := keystone.NewKeystoneAuthenticator(keystoneURL, keystoneCAFile)
	if err != nil {
		return nil, err
	}

	return basicauth.New(keystoneAuthenticator), nil
}

func newWebhookTokenAuthenticator(webhookConfigFile string, ttl time.Duration) (authenticator.Request, error) {
	webhookTokenAuthenticator, err := webhook.New(webhookConfigFile, ttl)
	if err != nil {
		return nil, err
	}

	return bearertoken.New(webhookTokenAuthenticator), nil
}
<commit_msg>Fix wrong comment in authn.go<commit_after>/*
Copyright 2014 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 authenticator

import (
	"time"

	"github.com/go-openapi/spec"

	"k8s.io/kubernetes/pkg/auth/authenticator"
	"k8s.io/kubernetes/pkg/auth/authenticator/bearertoken"
	"k8s.io/kubernetes/pkg/auth/group"
	"k8s.io/kubernetes/pkg/auth/user"
	"k8s.io/kubernetes/pkg/serviceaccount"
	certutil "k8s.io/kubernetes/pkg/util/cert"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/password/keystone"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/password/passwordfile"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/anonymous"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/basicauth"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/headerrequest"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/union"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/request/x509"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/anytoken"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/oidc"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/tokenfile"
	"k8s.io/kubernetes/plugin/pkg/auth/authenticator/token/webhook"
)

type RequestHeaderConfig struct {
	// UsernameHeaders are the headers to check (in order, case-insensitively) for an identity. The first header with a value wins.
	UsernameHeaders []string
	// ClientCA points to CA bundle file which is used verify the identity of the front proxy
	ClientCA string
	// AllowedClientNames is a list of common names that may be presented by the authenticating front proxy.  Empty means: accept any.
	AllowedClientNames []string
}

type AuthenticatorConfig struct {
	Anonymous                   bool
	AnyToken                    bool
	BasicAuthFile               string
	ClientCAFile                string
	TokenAuthFile               string
	OIDCIssuerURL               string
	OIDCClientID                string
	OIDCCAFile                  string
	OIDCUsernameClaim           string
	OIDCGroupsClaim             string
	ServiceAccountKeyFiles      []string
	ServiceAccountLookup        bool
	ServiceAccountTokenGetter   serviceaccount.ServiceAccountTokenGetter
	KeystoneURL                 string
	KeystoneCAFile              string
	WebhookTokenAuthnConfigFile string
	WebhookTokenAuthnCacheTTL   time.Duration

	RequestHeaderConfig *RequestHeaderConfig
}

// New returns an authenticator.Request or an error that supports the standard
// Kubernetes authentication mechanisms.
func New(config AuthenticatorConfig) (authenticator.Request, *spec.SecurityDefinitions, error) {
	var authenticators []authenticator.Request
	securityDefinitions := spec.SecurityDefinitions{}
	hasBasicAuth := false
	hasTokenAuth := false

	// front-proxy, BasicAuth methods, local first, then remote
	// Add the front proxy authenticator if requested
	if config.RequestHeaderConfig != nil {
		requestHeaderAuthenticator, err := headerrequest.NewSecure(
			config.RequestHeaderConfig.ClientCA,
			config.RequestHeaderConfig.AllowedClientNames,
			config.RequestHeaderConfig.UsernameHeaders,
		)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, requestHeaderAuthenticator)
	}

	if len(config.BasicAuthFile) > 0 {
		basicAuth, err := newAuthenticatorFromBasicAuthFile(config.BasicAuthFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, basicAuth)
		hasBasicAuth = true
	}
	if len(config.KeystoneURL) > 0 {
		keystoneAuth, err := newAuthenticatorFromKeystoneURL(config.KeystoneURL, config.KeystoneCAFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, keystoneAuth)
		hasBasicAuth = true
	}

	// X509 methods
	if len(config.ClientCAFile) > 0 {
		certAuth, err := newAuthenticatorFromClientCAFile(config.ClientCAFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, certAuth)
	}

	// Bearer token methods, local first, then remote
	if len(config.TokenAuthFile) > 0 {
		tokenAuth, err := newAuthenticatorFromTokenFile(config.TokenAuthFile)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, tokenAuth)
		hasTokenAuth = true
	}
	if len(config.ServiceAccountKeyFiles) > 0 {
		serviceAccountAuth, err := newServiceAccountAuthenticator(config.ServiceAccountKeyFiles, config.ServiceAccountLookup, config.ServiceAccountTokenGetter)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, serviceAccountAuth)
		hasTokenAuth = true
	}
	// NOTE(ericchiang): Keep the OpenID Connect after Service Accounts.
	//
	// Because both plugins verify JWTs whichever comes first in the union experiences
	// cache misses for all requests using the other. While the service account plugin
	// simply returns an error, the OpenID Connect plugin may query the provider to
	// update the keys, causing performance hits.
	if len(config.OIDCIssuerURL) > 0 && len(config.OIDCClientID) > 0 {
		oidcAuth, err := newAuthenticatorFromOIDCIssuerURL(config.OIDCIssuerURL, config.OIDCClientID, config.OIDCCAFile, config.OIDCUsernameClaim, config.OIDCGroupsClaim)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, oidcAuth)
		hasTokenAuth = true
	}
	if len(config.WebhookTokenAuthnConfigFile) > 0 {
		webhookTokenAuth, err := newWebhookTokenAuthenticator(config.WebhookTokenAuthnConfigFile, config.WebhookTokenAuthnCacheTTL)
		if err != nil {
			return nil, nil, err
		}
		authenticators = append(authenticators, webhookTokenAuth)
		hasTokenAuth = true
	}

	// always add anytoken last, so that every other token authenticator gets to try first
	if config.AnyToken {
		authenticators = append(authenticators, bearertoken.New(anytoken.AnyTokenAuthenticator{}))
		hasTokenAuth = true
	}

	if hasBasicAuth {
		securityDefinitions["HTTPBasic"] = &spec.SecurityScheme{
			SecuritySchemeProps: spec.SecuritySchemeProps{
				Type:        "basic",
				Description: "HTTP Basic authentication",
			},
		}
	}

	if hasTokenAuth {
		securityDefinitions["BearerToken"] = &spec.SecurityScheme{
			SecuritySchemeProps: spec.SecuritySchemeProps{
				Type:        "apiKey",
				Name:        "authorization",
				In:          "header",
				Description: "Bearer Token authentication",
			},
		}
	}

	if len(authenticators) == 0 {
		if config.Anonymous {
			return anonymous.NewAuthenticator(), &securityDefinitions, nil
		}
	}

	switch len(authenticators) {
	case 0:
		return nil, &securityDefinitions, nil
	}

	authenticator := union.New(authenticators...)

	authenticator = group.NewGroupAdder(authenticator, []string{user.AllAuthenticated})

	if config.Anonymous {
		// If the authenticator chain returns an error, return an error (don't consider a bad bearer token anonymous).
		authenticator = union.NewFailOnError(authenticator, anonymous.NewAuthenticator())
	}

	return authenticator, &securityDefinitions, nil
}

// IsValidServiceAccountKeyFile returns true if a valid public RSA key can be read from the given file
func IsValidServiceAccountKeyFile(file string) bool {
	_, err := serviceaccount.ReadPublicKeys(file)
	return err == nil
}

// newAuthenticatorFromBasicAuthFile returns an authenticator.Request or an error
func newAuthenticatorFromBasicAuthFile(basicAuthFile string) (authenticator.Request, error) {
	basicAuthenticator, err := passwordfile.NewCSV(basicAuthFile)
	if err != nil {
		return nil, err
	}

	return basicauth.New(basicAuthenticator), nil
}

// newAuthenticatorFromTokenFile returns an authenticator.Request or an error
func newAuthenticatorFromTokenFile(tokenAuthFile string) (authenticator.Request, error) {
	tokenAuthenticator, err := tokenfile.NewCSV(tokenAuthFile)
	if err != nil {
		return nil, err
	}

	return bearertoken.New(tokenAuthenticator), nil
}

// newAuthenticatorFromToken returns an authenticator.Request or an error
func NewAuthenticatorFromTokens(tokens map[string]*user.DefaultInfo) authenticator.Request {
	return bearertoken.New(tokenfile.New(tokens))
}

// newAuthenticatorFromOIDCIssuerURL returns an authenticator.Request or an error.
func newAuthenticatorFromOIDCIssuerURL(issuerURL, clientID, caFile, usernameClaim, groupsClaim string) (authenticator.Request, error) {
	tokenAuthenticator, err := oidc.New(oidc.OIDCOptions{
		IssuerURL:     issuerURL,
		ClientID:      clientID,
		CAFile:        caFile,
		UsernameClaim: usernameClaim,
		GroupsClaim:   groupsClaim,
	})
	if err != nil {
		return nil, err
	}

	return bearertoken.New(tokenAuthenticator), nil
}

// newServiceAccountAuthenticator returns an authenticator.Request or an error
func newServiceAccountAuthenticator(keyfiles []string, lookup bool, serviceAccountGetter serviceaccount.ServiceAccountTokenGetter) (authenticator.Request, error) {
	allPublicKeys := []interface{}{}
	for _, keyfile := range keyfiles {
		publicKeys, err := serviceaccount.ReadPublicKeys(keyfile)
		if err != nil {
			return nil, err
		}
		allPublicKeys = append(allPublicKeys, publicKeys...)
	}

	tokenAuthenticator := serviceaccount.JWTTokenAuthenticator(allPublicKeys, lookup, serviceAccountGetter)
	return bearertoken.New(tokenAuthenticator), nil
}

// newAuthenticatorFromClientCAFile returns an authenticator.Request or an error
func newAuthenticatorFromClientCAFile(clientCAFile string) (authenticator.Request, error) {
	roots, err := certutil.NewPool(clientCAFile)
	if err != nil {
		return nil, err
	}

	opts := x509.DefaultVerifyOptions()
	opts.Roots = roots

	return x509.New(opts, x509.CommonNameUserConversion), nil
}

// newAuthenticatorFromKeystoneURL returns an authenticator.Request or an error
func newAuthenticatorFromKeystoneURL(keystoneURL string, keystoneCAFile string) (authenticator.Request, error) {
	keystoneAuthenticator, err := keystone.NewKeystoneAuthenticator(keystoneURL, keystoneCAFile)
	if err != nil {
		return nil, err
	}

	return basicauth.New(keystoneAuthenticator), nil
}

func newWebhookTokenAuthenticator(webhookConfigFile string, ttl time.Duration) (authenticator.Request, error) {
	webhookTokenAuthenticator, err := webhook.New(webhookConfigFile, ttl)
	if err != nil {
		return nil, err
	}

	return bearertoken.New(webhookTokenAuthenticator), nil
}
<|endoftext|>


<commit_before>package connector

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/yaml"

	"github.com/datawire/ambassador/pkg/dtest"
	"github.com/datawire/ambassador/pkg/kates"
	"github.com/datawire/dlib/dgroup"
	"github.com/datawire/dlib/dlog"
	"github.com/datawire/telepresence2/v2/pkg/client"
	"github.com/datawire/telepresence2/v2/pkg/version"
)

var kubeconfig string
var namespace string
var registry string
var testVersion = "v0.1.2-test"

func TestMain(m *testing.M) {
	log.SetOutput(ioutil.Discard) // We want success or failure, not an abundance of output
	kubeconfig = dtest.Kubeconfig()
	namespace = fmt.Sprintf("telepresence-%d", os.Getpid())
	registry = dtest.DockerRegistry()
	version.Version = testVersion

	os.Setenv("DTEST_KUBECONFIG", kubeconfig)
	os.Setenv("KO_DOCKER_REPO", registry)
	os.Setenv("TELEPRESENCE_REGISTRY", registry)
	dtest.WithMachineLock(func() {
		capture(nil, "kubectl", "--kubeconfig", kubeconfig, "create", "namespace", namespace)
		defer capture(nil, "kubectl", "--kubeconfig", kubeconfig, "delete", "namespace", namespace, "--wait=false")
		os.Exit(m.Run())
	})
}

func showArgs(exe string, args []string) {
	fmt.Print("+ ")
	fmt.Print(exe)
	for _, arg := range args {
		fmt.Print(" ", arg)
	}
	fmt.Println()
}

func capture(t *testing.T, exe string, args ...string) string {
	showArgs(exe, args)
	cmd := exec.Command(exe, args...)
	out, err := cmd.CombinedOutput()
	sout := string(out)
	if err != nil {
		if t != nil {
			t.Fatalf("%s\n%v", sout, err)
		} else {
			log.Fatalf("%s\n%v", sout, err)
		}
	}
	return sout
}

var imageName string

func publishManager(t *testing.T) {
	t.Helper()
	if imageName != "" {
		return
	}

	// Check that the "ko" program exists, and adjust PATH as necessary.
	if info, err := os.Stat("../../../tools/bin/ko"); err != nil || !info.Mode().IsRegular() || (info.Mode().Perm()&0100) == 0 {
		t.Fatal("it looks like the ./tools/bin/ko executable wasn't built; be sure to build it with `make` before running `go test`!")
	}
	toolbindir, err := filepath.Abs("../../../tools/bin")
	if !assert.NoError(t, err) {
		t.FailNow()
	}
	os.Setenv("PATH", toolbindir+":"+os.Getenv("PATH"))

	// OK, now run "ko" and friends.
	cmd := exec.Command("ko", "publish", "--local", "./cmd/traffic")
	cmd.Dir = "../../.." // ko must be executed from root to find the .ko.yaml config
	stdout, err := cmd.Output()
	if err != nil {
		t.Fatalf("%s\n%v", stdout, err)
	}
	imageName = strings.TrimSpace(string(stdout))
	tag := fmt.Sprintf("%s/tel2:%s", registry, strings.TrimPrefix(client.Version(), "v"))
	capture(t, "docker", "tag", imageName, tag)
	capture(t, "docker", "push", tag)
}

func removeManager(t *testing.T) {
	// Remove service and deployment
	cmd := exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "delete", "svc,deployment", "traffic-manager")
	_, _ = cmd.Output()

	// Wait until getting them fails
	gone := false
	for cnt := 0; cnt < 10; cnt++ {
		cmd = exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "get", "deployment", "traffic-manager")
		if err := cmd.Run(); err != nil {
			gone = true
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if !gone {
		t.Fatal("timeout waiting for deployment to vanish")
	}
	gone = false
	for cnt := 0; cnt < 10; cnt++ {
		cmd = exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "get", "svc", "traffic-manager")
		if err := cmd.Run(); err != nil {
			gone = true
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if !gone {
		t.Fatal("timeout waiting for service to vanish")
	}
}

func Test_findTrafficManager_notPresent(t *testing.T) {
	ctx := dlog.NewTestContext(t, false)
	kc, err := newKCluster(ctx, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
	if err != nil {
		t.Fatal(err)
	}
	ti, err := newTrafficManagerInstaller(kc)
	if err != nil {
		t.Fatal(err)
	}
	version.Version = "v0.0.0-bogus"
	defer func() { version.Version = testVersion }()

	if dep := ti.findDeployment(managerAppName); dep != nil {
		t.Fatal("expected find to not find deployment")
	}
}

func Test_findTrafficManager_present(t *testing.T) {
	c, cancel := context.WithCancel(dlog.NewTestContext(t, false))
	g := dgroup.NewGroup(c, dgroup.GroupConfig{})
	env, err := client.LoadEnv(c)
	if err != nil {
		t.Fatal(err)
	}
	g.Go("test-present", func(c context.Context) error {
		defer cancel()
		publishManager(t)
		defer removeManager(t)
		kc, err := newKCluster(c, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
		if err != nil {
			return err
		}
		accWait := make(chan struct{})
		err = kc.startWatches(c, namespace, accWait)
		if err != nil {
			return err
		}
		<-accWait
		ti, err := newTrafficManagerInstaller(kc)
		if err != nil {
			return err
		}
		err = ti.createManagerDeployment(c, env)
		if err != nil {
			return err
		}
		for i := 0; i < 50; i++ {
			if dep := ti.findDeployment(managerAppName); dep != nil {
				return nil
			}
			time.Sleep(100 * time.Millisecond)
		}
		return errors.New("traffic-manager deployment not found")
	})
	if err := g.Wait(); err != nil {
		t.Fatal(err)
	}
}

func Test_ensureTrafficManager_notPresent(t *testing.T) {
	c := dlog.NewTestContext(t, false)
	publishManager(t)
	defer removeManager(t)
	env, err := client.LoadEnv(c)
	if err != nil {
		t.Fatal(err)
	}
	kc, err := newKCluster(c, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
	if err != nil {
		t.Fatal(err)
	}
	ti, err := newTrafficManagerInstaller(kc)
	if err != nil {
		t.Fatal(err)
	}
	if err := ti.ensureManager(c, env); err != nil {
		t.Fatal(err)
	}
}

func TestAddAgentToDeployment(t *testing.T) {
	type testcase struct {
		InputPortName   string
		InputDeployment *kates.Deployment
		InputService    *kates.Service

		OutputDeployment *kates.Deployment
		OutputService    *kates.Service
	}
	testcases := map[string]testcase{}

	fileinfos, err := ioutil.ReadDir("testdata/addAgentToDeployment")
	if err != nil {
		t.Fatal(err)
	}
	for _, fi := range fileinfos {
		if !strings.HasSuffix(fi.Name(), ".input.yaml") {
			continue
		}
		tcName := strings.TrimSuffix(fi.Name(), ".input.yaml")

		var tmp struct {
			Deployment *kates.Deployment `json:"deployment"`
			Service    *kates.Service    `json:"service"`
		}

		var tc testcase

		inBody, err := ioutil.ReadFile(filepath.Join("testdata/addAgentToDeployment", fi.Name()))
		if err != nil {
			t.Fatalf("%s.input.yaml: %v", tcName, err)
		}
		if err := yaml.Unmarshal(inBody, &tmp); err != nil {
			t.Fatalf("%s.input.yaml: %v", tcName, err)
		}
		tc.InputDeployment = tmp.Deployment
		tc.InputService = tmp.Service
		tmp.Deployment = nil
		tmp.Service = nil

		outBody, err := ioutil.ReadFile(filepath.Join("testdata/addAgentToDeployment", tcName+".output.yaml"))
		if err != nil {
			t.Fatalf("%s.output.yaml: %v", tcName, err)
		}
		if err := yaml.Unmarshal(outBody, &tmp); err != nil {
			t.Fatalf("%s.output.yaml: %v", tcName, err)
		}
		tc.OutputDeployment = tmp.Deployment
		tc.OutputService = tmp.Service

		testcases[tcName] = tc
	}

	env, err := client.LoadEnv(dlog.NewTestContext(t, true))
	if err != nil {
		t.Fatal(err)
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			ctx := dlog.NewTestContext(t, true)

			expectedDep := tc.OutputDeployment.DeepCopy()
			sanitizeDeployment(expectedDep)

			expectedSvc := tc.OutputService.DeepCopy()
			sanitizeService(expectedSvc)

			actualDep, actualSvc, actualErr := addAgentToDeployment(ctx,
				tc.InputPortName,
				agentImageName(ctx, env),
				tc.InputDeployment.DeepCopy(),
				[]*kates.Service{tc.InputService.DeepCopy()},
			)
			if !assert.NoError(t, actualErr) {
				return
			}

			sanitizeDeployment(actualDep)
			if actualSvc == nil {
				actualSvc = tc.InputService.DeepCopy()
			}
			sanitizeService(actualSvc)

			assert.Equal(t, expectedDep, actualDep)
			assert.Equal(t, expectedSvc, actualSvc)
		})
	}
}

func sanitizeDeployment(dep *kates.Deployment) {
	dep.ObjectMeta.ResourceVersion = ""
	dep.ObjectMeta.Generation = 0
	dep.ObjectMeta.CreationTimestamp = metav1.Time{}
	for i, c := range dep.Spec.Template.Spec.Containers {
		c.TerminationMessagePath = ""
		c.TerminationMessagePolicy = ""
		c.ImagePullPolicy = ""
		dep.Spec.Template.Spec.Containers[i] = c
	}
}

func sanitizeService(svc *kates.Service) {
	svc.ObjectMeta.ResourceVersion = ""
	svc.ObjectMeta.Generation = 0
	svc.ObjectMeta.CreationTimestamp = metav1.Time{}
}
<commit_msg>Move calls using *testing.T out of dgroup.Go() call.<commit_after>package connector

import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/yaml"

	"github.com/datawire/ambassador/pkg/dtest"
	"github.com/datawire/ambassador/pkg/kates"
	"github.com/datawire/dlib/dgroup"
	"github.com/datawire/dlib/dlog"
	"github.com/datawire/telepresence2/v2/pkg/client"
	"github.com/datawire/telepresence2/v2/pkg/version"
)

var kubeconfig string
var namespace string
var registry string
var testVersion = "v0.1.2-test"

func TestMain(m *testing.M) {
	log.SetOutput(ioutil.Discard) // We want success or failure, not an abundance of output
	kubeconfig = dtest.Kubeconfig()
	namespace = fmt.Sprintf("telepresence-%d", os.Getpid())
	registry = dtest.DockerRegistry()
	version.Version = testVersion

	os.Setenv("DTEST_KUBECONFIG", kubeconfig)
	os.Setenv("KO_DOCKER_REPO", registry)
	os.Setenv("TELEPRESENCE_REGISTRY", registry)
	dtest.WithMachineLock(func() {
		capture(nil, "kubectl", "--kubeconfig", kubeconfig, "create", "namespace", namespace)
		defer capture(nil, "kubectl", "--kubeconfig", kubeconfig, "delete", "namespace", namespace, "--wait=false")
		os.Exit(m.Run())
	})
}

func showArgs(exe string, args []string) {
	fmt.Print("+ ")
	fmt.Print(exe)
	for _, arg := range args {
		fmt.Print(" ", arg)
	}
	fmt.Println()
}

func capture(t *testing.T, exe string, args ...string) string {
	showArgs(exe, args)
	cmd := exec.Command(exe, args...)
	out, err := cmd.CombinedOutput()
	sout := string(out)
	if err != nil {
		if t != nil {
			t.Fatalf("%s\n%v", sout, err)
		} else {
			log.Fatalf("%s\n%v", sout, err)
		}
	}
	return sout
}

var imageName string

func publishManager(t *testing.T) {
	t.Helper()
	if imageName != "" {
		return
	}

	// Check that the "ko" program exists, and adjust PATH as necessary.
	if info, err := os.Stat("../../../tools/bin/ko"); err != nil || !info.Mode().IsRegular() || (info.Mode().Perm()&0100) == 0 {
		t.Fatal("it looks like the ./tools/bin/ko executable wasn't built; be sure to build it with `make` before running `go test`!")
	}
	toolbindir, err := filepath.Abs("../../../tools/bin")
	if !assert.NoError(t, err) {
		t.FailNow()
	}
	os.Setenv("PATH", toolbindir+":"+os.Getenv("PATH"))

	// OK, now run "ko" and friends.
	cmd := exec.Command("ko", "publish", "--local", "./cmd/traffic")
	cmd.Dir = "../../.." // ko must be executed from root to find the .ko.yaml config
	errCapture := bytes.Buffer{}
	cmd.Stderr = &errCapture
	stdout, err := cmd.Output()
	if err != nil {
		t.Fatalf("%s\n%v", errCapture.String(), err)
	}
	imageName = strings.TrimSpace(string(stdout))
	tag := fmt.Sprintf("%s/tel2:%s", registry, strings.TrimPrefix(client.Version(), "v"))
	capture(t, "docker", "tag", imageName, tag)
	capture(t, "docker", "push", tag)
}

func removeManager(t *testing.T) {
	// Remove service and deployment
	cmd := exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "delete", "svc,deployment", "traffic-manager")
	_, _ = cmd.Output()

	// Wait until getting them fails
	gone := false
	for cnt := 0; cnt < 10; cnt++ {
		cmd = exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "get", "deployment", "traffic-manager")
		if err := cmd.Run(); err != nil {
			gone = true
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if !gone {
		t.Fatal("timeout waiting for deployment to vanish")
	}
	gone = false
	for cnt := 0; cnt < 10; cnt++ {
		cmd = exec.Command("kubectl", "--kubeconfig", kubeconfig, "--namespace", namespace, "get", "svc", "traffic-manager")
		if err := cmd.Run(); err != nil {
			gone = true
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if !gone {
		t.Fatal("timeout waiting for service to vanish")
	}
}

func Test_findTrafficManager_notPresent(t *testing.T) {
	ctx := dlog.NewTestContext(t, false)
	kc, err := newKCluster(ctx, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
	if err != nil {
		t.Fatal(err)
	}
	ti, err := newTrafficManagerInstaller(kc)
	if err != nil {
		t.Fatal(err)
	}
	version.Version = "v0.0.0-bogus"
	defer func() { version.Version = testVersion }()

	if dep := ti.findDeployment(managerAppName); dep != nil {
		t.Fatal("expected find to not find deployment")
	}
}

func Test_findTrafficManager_present(t *testing.T) {
	c := dlog.NewTestContext(t, false)
	publishManager(t)
	defer removeManager(t)

	env, err := client.LoadEnv(c)
	if err != nil {
		t.Fatal(err)
	}

	c, cancel := context.WithCancel(c)
	g := dgroup.NewGroup(c, dgroup.GroupConfig{})
	g.Go("test-present", func(c context.Context) (err error) {
		// Kill sibling go-routines
		defer cancel()

		kc, err := newKCluster(c, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
		if err != nil {
			return err
		}
		accWait := make(chan struct{})
		err = kc.startWatches(c, namespace, accWait)
		if err != nil {
			return err
		}
		<-accWait
		ti, err := newTrafficManagerInstaller(kc)
		if err != nil {
			return err
		}
		err = ti.createManagerDeployment(c, env)
		if err != nil {
			return err
		}
		for i := 0; i < 50; i++ {
			if dep := ti.findDeployment(managerAppName); dep != nil {
				return nil
			}
			time.Sleep(100 * time.Millisecond)
		}
		return errors.New("traffic-manager deployment not found")
	})
	if err := g.Wait(); err != nil {
		t.Fatal(err)
	}
}

func Test_ensureTrafficManager_notPresent(t *testing.T) {
	c := dlog.NewTestContext(t, false)
	publishManager(t)
	defer removeManager(t)
	env, err := client.LoadEnv(c)
	if err != nil {
		t.Fatal(err)
	}
	kc, err := newKCluster(c, map[string]string{"kubeconfig": kubeconfig, "namespace": namespace}, nil)
	if err != nil {
		t.Fatal(err)
	}
	ti, err := newTrafficManagerInstaller(kc)
	if err != nil {
		t.Fatal(err)
	}
	if err := ti.ensureManager(c, env); err != nil {
		t.Fatal(err)
	}
}

func TestAddAgentToDeployment(t *testing.T) {
	type testcase struct {
		InputPortName   string
		InputDeployment *kates.Deployment
		InputService    *kates.Service

		OutputDeployment *kates.Deployment
		OutputService    *kates.Service
	}
	testcases := map[string]testcase{}

	fileinfos, err := ioutil.ReadDir("testdata/addAgentToDeployment")
	if err != nil {
		t.Fatal(err)
	}
	for _, fi := range fileinfos {
		if !strings.HasSuffix(fi.Name(), ".input.yaml") {
			continue
		}
		tcName := strings.TrimSuffix(fi.Name(), ".input.yaml")

		var tmp struct {
			Deployment *kates.Deployment `json:"deployment"`
			Service    *kates.Service    `json:"service"`
		}

		var tc testcase

		inBody, err := ioutil.ReadFile(filepath.Join("testdata/addAgentToDeployment", fi.Name()))
		if err != nil {
			t.Fatalf("%s.input.yaml: %v", tcName, err)
		}
		if err := yaml.Unmarshal(inBody, &tmp); err != nil {
			t.Fatalf("%s.input.yaml: %v", tcName, err)
		}
		tc.InputDeployment = tmp.Deployment
		tc.InputService = tmp.Service
		tmp.Deployment = nil
		tmp.Service = nil

		outBody, err := ioutil.ReadFile(filepath.Join("testdata/addAgentToDeployment", tcName+".output.yaml"))
		if err != nil {
			t.Fatalf("%s.output.yaml: %v", tcName, err)
		}
		if err := yaml.Unmarshal(outBody, &tmp); err != nil {
			t.Fatalf("%s.output.yaml: %v", tcName, err)
		}
		tc.OutputDeployment = tmp.Deployment
		tc.OutputService = tmp.Service

		testcases[tcName] = tc
	}

	env, err := client.LoadEnv(dlog.NewTestContext(t, true))
	if err != nil {
		t.Fatal(err)
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			ctx := dlog.NewTestContext(t, true)

			expectedDep := tc.OutputDeployment.DeepCopy()
			sanitizeDeployment(expectedDep)

			expectedSvc := tc.OutputService.DeepCopy()
			sanitizeService(expectedSvc)

			actualDep, actualSvc, actualErr := addAgentToDeployment(ctx,
				tc.InputPortName,
				agentImageName(ctx, env),
				tc.InputDeployment.DeepCopy(),
				[]*kates.Service{tc.InputService.DeepCopy()},
			)
			if !assert.NoError(t, actualErr) {
				return
			}

			sanitizeDeployment(actualDep)
			if actualSvc == nil {
				actualSvc = tc.InputService.DeepCopy()
			}
			sanitizeService(actualSvc)

			assert.Equal(t, expectedDep, actualDep)
			assert.Equal(t, expectedSvc, actualSvc)
		})
	}
}

func sanitizeDeployment(dep *kates.Deployment) {
	dep.ObjectMeta.ResourceVersion = ""
	dep.ObjectMeta.Generation = 0
	dep.ObjectMeta.CreationTimestamp = metav1.Time{}
	for i, c := range dep.Spec.Template.Spec.Containers {
		c.TerminationMessagePath = ""
		c.TerminationMessagePolicy = ""
		c.ImagePullPolicy = ""
		dep.Spec.Template.Spec.Containers[i] = c
	}
}

func sanitizeService(svc *kates.Service) {
	svc.ObjectMeta.ResourceVersion = ""
	svc.ObjectMeta.Generation = 0
	svc.ObjectMeta.CreationTimestamp = metav1.Time{}
}
<|endoftext|>


<commit_before>package container_runtime

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/werf/lockgate"
	"github.com/werf/werf/pkg/werf"

	"github.com/docker/docker/api/types"

	"github.com/werf/logboek"

	"github.com/werf/werf/pkg/docker"
	"github.com/werf/werf/pkg/image"
)

type StageImage struct {
	*baseImage
	fromImage              *StageImage
	container              *StageImageContainer
	buildImage             *buildImage
	dockerfileImageBuilder *DockerfileImageBuilder
}

func NewStageImage(fromImage *StageImage, name string, localDockerServerRuntime *LocalDockerServerRuntime) *StageImage {
	stage := &StageImage{}
	stage.baseImage = newBaseImage(name, localDockerServerRuntime)
	stage.fromImage = fromImage
	stage.container = newStageImageContainer(stage)
	return stage
}

func (i *StageImage) Inspect() *types.ImageInspect {
	return i.inspect
}

func (i *StageImage) BuilderContainer() BuilderContainer {
	return &StageImageBuilderContainer{i}
}

func (i *StageImage) Container() Container {
	return i.container
}

func (i *StageImage) GetID() string {
	if i.buildImage != nil {
		return i.buildImage.Name()
	} else {
		return i.baseImage.GetStageDescription().Info.ID
	}
}

func (i *StageImage) Build(ctx context.Context, options BuildOptions) error {
	if i.dockerfileImageBuilder != nil {
		if err := i.dockerfileImageBuilder.Build(ctx); err != nil {
			return err
		}
	} else {
		containerLockName := ContainerLockName(i.container.Name())
		if _, lock, err := werf.AcquireHostLock(ctx, containerLockName, lockgate.AcquireOptions{}); err != nil {
			return fmt.Errorf("failed to lock %s: %s", containerLockName, err)
		} else {
			defer werf.ReleaseHostLock(lock)
		}

		if debugDockerRunCommand() {
			runArgs, err := i.container.prepareRunArgs(ctx)
			if err != nil {
				return err
			}

			fmt.Printf("Docker run command:\ndocker run %s\n", strings.Join(runArgs, " "))

			if len(i.container.prepareAllRunCommands()) != 0 {
				fmt.Printf("Decoded command:\n%s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))
			}
		}

		if containerRunErr := i.container.run(ctx); containerRunErr != nil {
			if strings.HasPrefix(containerRunErr.Error(), "container run failed") {
				if options.IntrospectBeforeError {
					logboek.Context(ctx).Default().LogFDetails("Launched command: %s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))

					if err := logboek.Context(ctx).Streams().DoErrorWithoutProxyStreamDataFormatting(func() error {
						return i.introspectBefore(ctx)
					}); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}
				} else if options.IntrospectAfterError {
					if err := i.Commit(ctx); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}

					logboek.Context(ctx).Default().LogFDetails("Launched command: %s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))

					if err := logboek.Context(ctx).Streams().DoErrorWithoutProxyStreamDataFormatting(func() error {
						return i.Introspect(ctx)
					}); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}
				}

				if err := i.container.rm(ctx); err != nil {
					return fmt.Errorf("introspect error failed: %s", err)
				}
			}

			return containerRunErr
		}

		if err := i.Commit(ctx); err != nil {
			return err
		}

		if err := i.container.rm(ctx); err != nil {
			return err
		}
	}

	if inspect, err := i.LocalDockerServerRuntime.GetImageInspect(ctx, i.MustGetBuiltId()); err != nil {
		return err
	} else {
		i.SetInspect(inspect)
		i.SetStageDescription(&image.StageDescription{
			StageID: nil, // stage id does not available at the moment
			Info:    image.NewInfoFromInspect(i.Name(), inspect),
		})
	}

	return nil
}

func (i *StageImage) Commit(ctx context.Context) error {
	builtId, err := i.container.commit(ctx)
	if err != nil {
		return err
	}

	i.buildImage = newBuildImage(builtId, i.LocalDockerServerRuntime)

	return nil
}

func (i *StageImage) Introspect(ctx context.Context) error {
	if err := i.container.introspect(ctx); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) introspectBefore(ctx context.Context) error {
	if err := i.container.introspectBefore(ctx); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) MustResetInspect(ctx context.Context) error {
	if i.buildImage != nil {
		return i.buildImage.MustResetInspect(ctx)
	} else {
		return i.baseImage.MustResetInspect(ctx)
	}
}

func (i *StageImage) GetInspect() *types.ImageInspect {
	if i.buildImage != nil {
		return i.buildImage.GetInspect()
	} else {
		return i.baseImage.GetInspect()
	}

}

func (i *StageImage) MustGetBuiltId() string {
	builtId := i.GetBuiltId()
	if builtId == "" {
		panic(fmt.Sprintf("image %s built id is not available", i.Name()))
	}
	return builtId
}

func (i *StageImage) GetBuiltId() string {
	if i.dockerfileImageBuilder != nil {
		return i.dockerfileImageBuilder.GetBuiltId()
	} else if i.buildImage != nil {
		return i.buildImage.Name()
	} else {
		return ""
	}
}

func (i *StageImage) TagBuiltImage(ctx context.Context, name string) error {
	return docker.CliTag(ctx, i.MustGetBuiltId(), i.name)
}

func (i *StageImage) Tag(ctx context.Context, name string) error {
	return docker.CliTag(ctx, i.GetID(), name)
}

func (i *StageImage) Pull(ctx context.Context) error {
	if err := docker.CliPullWithRetries(ctx, i.name); err != nil {
		return err
	}

	i.baseImage.UnsetInspect()

	return nil
}

func (i *StageImage) Push(ctx context.Context) error {
	return docker.CliPushWithRetries(ctx, i.name)
}

func (i *StageImage) Import(ctx context.Context, name string) error {
	importedImage := newBaseImage(name, i.LocalDockerServerRuntime)

	if err := docker.CliPullWithRetries(ctx, name); err != nil {
		return err
	}

	importedImageId := importedImage.GetStageDescription().Info.ID

	if err := docker.CliTag(ctx, importedImageId, i.name); err != nil {
		return err
	}

	if err := docker.CliRmi(ctx, name); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) Export(ctx context.Context, name string) error {
	if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Tagging %s", name)).DoError(func() error {
		return i.Tag(ctx, name)
	}); err != nil {
		return err
	}

	defer func() {
		if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Untagging %s", name)).DoError(func() error {
			return docker.CliRmi(ctx, name)
		}); err != nil {
			// TODO: errored image state
			logboek.Context(ctx).Error().LogF("Unable to remote temporary image %q: %s", name, err)
		}
	}()

	if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Pushing %s", name)).DoError(func() error {
		return docker.CliPushWithRetries(ctx, name)
	}); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) DockerfileImageBuilder() *DockerfileImageBuilder {
	if i.dockerfileImageBuilder == nil {
		i.dockerfileImageBuilder = NewDockerfileImageBuilder()
	}
	return i.dockerfileImageBuilder
}

func debugDockerRunCommand() bool {
	return os.Getenv("WERF_DEBUG_DOCKER_RUN_COMMAND") == "1"
}
<commit_msg>Fix build panic 'invalid memory address or nil pointer dereference' pkg/build/build_phase.go:422<commit_after>package container_runtime

import (
	"context"
	"fmt"
	"os"
	"strings"

	"github.com/werf/lockgate"
	"github.com/werf/werf/pkg/werf"

	"github.com/docker/docker/api/types"

	"github.com/werf/logboek"

	"github.com/werf/werf/pkg/docker"
	"github.com/werf/werf/pkg/image"
)

type StageImage struct {
	*baseImage
	fromImage              *StageImage
	container              *StageImageContainer
	buildImage             *buildImage
	dockerfileImageBuilder *DockerfileImageBuilder
}

func NewStageImage(fromImage *StageImage, name string, localDockerServerRuntime *LocalDockerServerRuntime) *StageImage {
	stage := &StageImage{}
	stage.baseImage = newBaseImage(name, localDockerServerRuntime)
	stage.fromImage = fromImage
	stage.container = newStageImageContainer(stage)
	return stage
}

func (i *StageImage) Inspect() *types.ImageInspect {
	return i.inspect
}

func (i *StageImage) BuilderContainer() BuilderContainer {
	return &StageImageBuilderContainer{i}
}

func (i *StageImage) Container() Container {
	return i.container
}

func (i *StageImage) GetID() string {
	if i.buildImage != nil {
		return i.buildImage.Name()
	} else {
		return i.baseImage.GetStageDescription().Info.ID
	}
}

func (i *StageImage) Build(ctx context.Context, options BuildOptions) error {
	if i.dockerfileImageBuilder != nil {
		if err := i.dockerfileImageBuilder.Build(ctx); err != nil {
			return err
		}
	} else {
		containerLockName := ContainerLockName(i.container.Name())
		if _, lock, err := werf.AcquireHostLock(ctx, containerLockName, lockgate.AcquireOptions{}); err != nil {
			return fmt.Errorf("failed to lock %s: %s", containerLockName, err)
		} else {
			defer werf.ReleaseHostLock(lock)
		}

		if debugDockerRunCommand() {
			runArgs, err := i.container.prepareRunArgs(ctx)
			if err != nil {
				return err
			}

			fmt.Printf("Docker run command:\ndocker run %s\n", strings.Join(runArgs, " "))

			if len(i.container.prepareAllRunCommands()) != 0 {
				fmt.Printf("Decoded command:\n%s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))
			}
		}

		if containerRunErr := i.container.run(ctx); containerRunErr != nil {
			if strings.HasPrefix(containerRunErr.Error(), "container run failed") {
				if options.IntrospectBeforeError {
					logboek.Context(ctx).Default().LogFDetails("Launched command: %s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))

					if err := logboek.Context(ctx).Streams().DoErrorWithoutProxyStreamDataFormatting(func() error {
						return i.introspectBefore(ctx)
					}); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}
				} else if options.IntrospectAfterError {
					if err := i.Commit(ctx); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}

					logboek.Context(ctx).Default().LogFDetails("Launched command: %s\n", strings.Join(i.container.prepareAllRunCommands(), " && "))

					if err := logboek.Context(ctx).Streams().DoErrorWithoutProxyStreamDataFormatting(func() error {
						return i.Introspect(ctx)
					}); err != nil {
						return fmt.Errorf("introspect error failed: %s", err)
					}
				}

				if err := i.container.rm(ctx); err != nil {
					return fmt.Errorf("introspect error failed: %s", err)
				}
			}

			return containerRunErr
		}

		if err := i.Commit(ctx); err != nil {
			return err
		}

		if err := i.container.rm(ctx); err != nil {
			return err
		}
	}

	if inspect, err := i.LocalDockerServerRuntime.GetImageInspect(ctx, i.MustGetBuiltId()); err != nil {
		return err
	} else {
		i.baseImage.SetInspect(inspect)
		i.buildImage.SetInspect(inspect)

		i.SetStageDescription(&image.StageDescription{
			StageID: nil, // stage id does not available at the moment
			Info:    image.NewInfoFromInspect(i.Name(), inspect),
		})
	}

	return nil
}

func (i *StageImage) Commit(ctx context.Context) error {
	builtId, err := i.container.commit(ctx)
	if err != nil {
		return err
	}

	i.buildImage = newBuildImage(builtId, i.LocalDockerServerRuntime)

	return nil
}

func (i *StageImage) Introspect(ctx context.Context) error {
	if err := i.container.introspect(ctx); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) introspectBefore(ctx context.Context) error {
	if err := i.container.introspectBefore(ctx); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) MustResetInspect(ctx context.Context) error {
	if i.buildImage != nil {
		return i.buildImage.MustResetInspect(ctx)
	} else {
		return i.baseImage.MustResetInspect(ctx)
	}
}

func (i *StageImage) GetInspect() *types.ImageInspect {
	if i.buildImage != nil {
		return i.buildImage.GetInspect()
	} else {
		return i.baseImage.GetInspect()
	}

}

func (i *StageImage) MustGetBuiltId() string {
	builtId := i.GetBuiltId()
	if builtId == "" {
		panic(fmt.Sprintf("image %s built id is not available", i.Name()))
	}
	return builtId
}

func (i *StageImage) GetBuiltId() string {
	if i.dockerfileImageBuilder != nil {
		return i.dockerfileImageBuilder.GetBuiltId()
	} else if i.buildImage != nil {
		return i.buildImage.Name()
	} else {
		return ""
	}
}

func (i *StageImage) TagBuiltImage(ctx context.Context, name string) error {
	return docker.CliTag(ctx, i.MustGetBuiltId(), i.name)
}

func (i *StageImage) Tag(ctx context.Context, name string) error {
	return docker.CliTag(ctx, i.GetID(), name)
}

func (i *StageImage) Pull(ctx context.Context) error {
	if err := docker.CliPullWithRetries(ctx, i.name); err != nil {
		return err
	}

	i.baseImage.UnsetInspect()

	return nil
}

func (i *StageImage) Push(ctx context.Context) error {
	return docker.CliPushWithRetries(ctx, i.name)
}

func (i *StageImage) Import(ctx context.Context, name string) error {
	importedImage := newBaseImage(name, i.LocalDockerServerRuntime)

	if err := docker.CliPullWithRetries(ctx, name); err != nil {
		return err
	}

	importedImageId := importedImage.GetStageDescription().Info.ID

	if err := docker.CliTag(ctx, importedImageId, i.name); err != nil {
		return err
	}

	if err := docker.CliRmi(ctx, name); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) Export(ctx context.Context, name string) error {
	if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Tagging %s", name)).DoError(func() error {
		return i.Tag(ctx, name)
	}); err != nil {
		return err
	}

	defer func() {
		if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Untagging %s", name)).DoError(func() error {
			return docker.CliRmi(ctx, name)
		}); err != nil {
			// TODO: errored image state
			logboek.Context(ctx).Error().LogF("Unable to remote temporary image %q: %s", name, err)
		}
	}()

	if err := logboek.Context(ctx).Info().LogProcess(fmt.Sprintf("Pushing %s", name)).DoError(func() error {
		return docker.CliPushWithRetries(ctx, name)
	}); err != nil {
		return err
	}

	return nil
}

func (i *StageImage) DockerfileImageBuilder() *DockerfileImageBuilder {
	if i.dockerfileImageBuilder == nil {
		i.dockerfileImageBuilder = NewDockerfileImageBuilder()
	}
	return i.dockerfileImageBuilder
}

func debugDockerRunCommand() bool {
	return os.Getenv("WERF_DEBUG_DOCKER_RUN_COMMAND") == "1"
}
<|endoftext|>


<commit_before>package auth

import (
	"testing"
	"time"

	"github.com/grafana/grafana/pkg/setting"

	"github.com/grafana/grafana/pkg/log"
	"github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/services/sqlstore"
	. "github.com/smartystreets/goconvey/convey"
)

func TestUserAuthToken(t *testing.T) {
	Convey("Test user auth token", t, func() {
		ctx := createTestContext(t)
		userAuthTokenService := ctx.tokenService
		userID := int64(10)

		t := time.Date(2018, 12, 13, 13, 45, 0, 0, time.UTC)
		now = func() time.Time {
			return t
		}

		Convey("When creating token", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)
			So(token.AuthTokenSeen, ShouldBeFalse)

			Convey("When lookup unhashed token should return user auth token", func() {
				LookupToken, err := userAuthTokenService.LookupToken(token.UnhashedToken)
				So(err, ShouldBeNil)
				So(LookupToken, ShouldNotBeNil)
				So(LookupToken.UserId, ShouldEqual, userID)
				So(LookupToken.AuthTokenSeen, ShouldBeTrue)

				storedAuthToken, err := ctx.getAuthTokenByID(LookupToken.Id)
				So(err, ShouldBeNil)
				So(storedAuthToken, ShouldNotBeNil)
				So(storedAuthToken.AuthTokenSeen, ShouldBeTrue)
			})

			Convey("When lookup hashed token should return user auth token not found error", func() {
				LookupToken, err := userAuthTokenService.LookupToken(token.AuthToken)
				So(err, ShouldEqual, ErrAuthTokenNotFound)
				So(LookupToken, ShouldBeNil)
			})
		})

		Convey("expires correctly", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)

			_, err = userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)

			now = func() time.Time {
				return t.Add(time.Hour)
			}

			refreshed, err := userAuthTokenService.RefreshToken(token, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			_, err = userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)

			stillGood, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)
			So(stillGood, ShouldNotBeNil)

			now = func() time.Time {
				return t.Add(24 * 7 * time.Hour)
			}
			notGood, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldEqual, ErrAuthTokenNotFound)
			So(notGood, ShouldBeNil)
		})

		Convey("can properly rotate tokens", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)

			prevToken := token.AuthToken
			unhashedPrev := token.UnhashedToken

			refreshed, err := userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeFalse)

			updated, err := ctx.markAuthTokenAsSeen(token.Id)
			So(err, ShouldBeNil)
			So(updated, ShouldBeTrue)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)

			now = func() time.Time {
				return t.Add(time.Hour)
			}

			refreshed, err = userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			unhashedToken := token.UnhashedToken

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)
			token.UnhashedToken = unhashedToken

			So(token.RotatedAt, ShouldEqual, now().Unix())
			So(token.ClientIp, ShouldEqual, "192.168.10.12")
			So(token.UserAgent, ShouldEqual, "a new user agent")
			So(token.AuthTokenSeen, ShouldBeFalse)
			So(token.SeenAt, ShouldEqual, 0)
			So(token.PrevAuthToken, ShouldEqual, prevToken)

			// ability to auth using an old token

			lookedUp, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)
			So(lookedUp.SeenAt, ShouldEqual, now().Unix())

			lookedUp, err = userAuthTokenService.LookupToken(unhashedPrev)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.Id, ShouldEqual, token.Id)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)

			now = func() time.Time {
				return t.Add(time.Hour + (2 * time.Minute))
			}

			lookedUp, err = userAuthTokenService.LookupToken(unhashedPrev)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)

			lookedUp, err = ctx.getAuthTokenByID(lookedUp.Id)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeFalse)

			refreshed, err = userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)
			So(token.SeenAt, ShouldEqual, 0)
		})

		Convey("keeps prev token valid for 1 minute after it is confirmed", func() {

		})

		Convey("will not mark token unseen when prev and current are the same", func() {

		})

		Reset(func() {
			now = time.Now
		})
	})
}

func createTestContext(t *testing.T) *testContext {
	t.Helper()

	sqlstore := sqlstore.InitTestDB(t)
	tokenService := &UserAuthTokenService{
		SQLStore: sqlstore,
		log:      log.New("test-logger"),
	}

	RotateTime = 10 * time.Minute
	UrgentRotateTime = time.Minute
	setting.LogInRememberDays = 7

	return &testContext{
		sqlstore:     sqlstore,
		tokenService: tokenService,
	}
}

type testContext struct {
	sqlstore     *sqlstore.SqlStore
	tokenService *UserAuthTokenService
}

func (c *testContext) getAuthTokenByID(id int64) (*models.UserAuthToken, error) {
	sess := c.sqlstore.NewSession()
	var t models.UserAuthToken
	found, err := sess.ID(id).Get(&t)
	if err != nil || !found {
		return nil, err
	}

	return &t, nil
}

func (c *testContext) markAuthTokenAsSeen(id int64) (bool, error) {
	sess := c.sqlstore.NewSession()
	res, err := sess.Exec("UPDATE user_auth_token SET auth_token_seen = ? WHERE id = ?", c.sqlstore.Dialect.BooleanStr(true), id)
	if err != nil {
		return false, err
	}

	rowsAffected, err := res.RowsAffected()
	if err != nil {
		return false, err
	}
	return rowsAffected == 1, nil
}
<commit_msg>fix tests after renaming now<commit_after>package auth

import (
	"testing"
	"time"

	"github.com/grafana/grafana/pkg/setting"

	"github.com/grafana/grafana/pkg/log"
	"github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/services/sqlstore"
	. "github.com/smartystreets/goconvey/convey"
)

func TestUserAuthToken(t *testing.T) {
	Convey("Test user auth token", t, func() {
		ctx := createTestContext(t)
		userAuthTokenService := ctx.tokenService
		userID := int64(10)

		t := time.Date(2018, 12, 13, 13, 45, 0, 0, time.UTC)
		getTime = func() time.Time {
			return t
		}

		Convey("When creating token", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)
			So(token.AuthTokenSeen, ShouldBeFalse)

			Convey("When lookup unhashed token should return user auth token", func() {
				LookupToken, err := userAuthTokenService.LookupToken(token.UnhashedToken)
				So(err, ShouldBeNil)
				So(LookupToken, ShouldNotBeNil)
				So(LookupToken.UserId, ShouldEqual, userID)
				So(LookupToken.AuthTokenSeen, ShouldBeTrue)

				storedAuthToken, err := ctx.getAuthTokenByID(LookupToken.Id)
				So(err, ShouldBeNil)
				So(storedAuthToken, ShouldNotBeNil)
				So(storedAuthToken.AuthTokenSeen, ShouldBeTrue)
			})

			Convey("When lookup hashed token should return user auth token not found error", func() {
				LookupToken, err := userAuthTokenService.LookupToken(token.AuthToken)
				So(err, ShouldEqual, ErrAuthTokenNotFound)
				So(LookupToken, ShouldBeNil)
			})
		})

		Convey("expires correctly", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)

			_, err = userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)

			getTime = func() time.Time {
				return t.Add(time.Hour)
			}

			refreshed, err := userAuthTokenService.RefreshToken(token, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			_, err = userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)

			stillGood, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)
			So(stillGood, ShouldNotBeNil)

			getTime = func() time.Time {
				return t.Add(24 * 7 * time.Hour)
			}
			notGood, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldEqual, ErrAuthTokenNotFound)
			So(notGood, ShouldBeNil)
		})

		Convey("can properly rotate tokens", func() {
			token, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)

			prevToken := token.AuthToken
			unhashedPrev := token.UnhashedToken

			refreshed, err := userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeFalse)

			updated, err := ctx.markAuthTokenAsSeen(token.Id)
			So(err, ShouldBeNil)
			So(updated, ShouldBeTrue)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)

			getTime = func() time.Time {
				return t.Add(time.Hour)
			}

			refreshed, err = userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			unhashedToken := token.UnhashedToken

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)
			token.UnhashedToken = unhashedToken

			So(token.RotatedAt, ShouldEqual, getTime().Unix())
			So(token.ClientIp, ShouldEqual, "192.168.10.12")
			So(token.UserAgent, ShouldEqual, "a new user agent")
			So(token.AuthTokenSeen, ShouldBeFalse)
			So(token.SeenAt, ShouldEqual, 0)
			So(token.PrevAuthToken, ShouldEqual, prevToken)

			// ability to auth using an old token

			lookedUp, err := userAuthTokenService.LookupToken(token.UnhashedToken)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)
			So(lookedUp.SeenAt, ShouldEqual, getTime().Unix())

			lookedUp, err = userAuthTokenService.LookupToken(unhashedPrev)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.Id, ShouldEqual, token.Id)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)

			getTime = func() time.Time {
				return t.Add(time.Hour + (2 * time.Minute))
			}

			lookedUp, err = userAuthTokenService.LookupToken(unhashedPrev)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeTrue)

			lookedUp, err = ctx.getAuthTokenByID(lookedUp.Id)
			So(err, ShouldBeNil)
			So(lookedUp, ShouldNotBeNil)
			So(lookedUp.AuthTokenSeen, ShouldBeFalse)

			refreshed, err = userAuthTokenService.RefreshToken(token, "192.168.10.12:1234", "a new user agent")
			So(err, ShouldBeNil)
			So(refreshed, ShouldBeTrue)

			token, err = ctx.getAuthTokenByID(token.Id)
			So(err, ShouldBeNil)
			So(token, ShouldNotBeNil)
			So(token.SeenAt, ShouldEqual, 0)
		})

		Convey("keeps prev token valid for 1 minute after it is confirmed", func() {

		})

		Convey("will not mark token unseen when prev and current are the same", func() {

		})

		Reset(func() {
			getTime = time.Now
		})
	})
}

func createTestContext(t *testing.T) *testContext {
	t.Helper()

	sqlstore := sqlstore.InitTestDB(t)
	tokenService := &UserAuthTokenService{
		SQLStore: sqlstore,
		log:      log.New("test-logger"),
	}

	RotateTime = 10 * time.Minute
	UrgentRotateTime = time.Minute
	setting.LogInRememberDays = 7

	return &testContext{
		sqlstore:     sqlstore,
		tokenService: tokenService,
	}
}

type testContext struct {
	sqlstore     *sqlstore.SqlStore
	tokenService *UserAuthTokenService
}

func (c *testContext) getAuthTokenByID(id int64) (*models.UserAuthToken, error) {
	sess := c.sqlstore.NewSession()
	var t models.UserAuthToken
	found, err := sess.ID(id).Get(&t)
	if err != nil || !found {
		return nil, err
	}

	return &t, nil
}

func (c *testContext) markAuthTokenAsSeen(id int64) (bool, error) {
	sess := c.sqlstore.NewSession()
	res, err := sess.Exec("UPDATE user_auth_token SET auth_token_seen = ? WHERE id = ?", c.sqlstore.Dialect.BooleanStr(true), id)
	if err != nil {
		return false, err
	}

	rowsAffected, err := res.RowsAffected()
	if err != nil {
		return false, err
	}
	return rowsAffected == 1, nil
}
<|endoftext|>


<commit_before>// Copyright 2015 Luke Shumaker

package httpentity

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/url"
	"net/http"
)

func (r Response) WriteEntity(w io.Writer) error {
	if r.entity == nil {
		return nil
	}
	mimetype, _, _ := mime.ParseMediaType(r.Headers.Get("Content-Type"))
	encoders := r.entity.Encoders()
	return encoders[mimetype](w)
}

func ReadEntity(r io.Reader, contenttype string) (interface{}, error) {
	mimetype, params, err := mime.ParseMediaType(contenttype)
	if err != nil {
		return nil, err
	}
	switch mimetype {
	case "application/x-www-form-urlencoded":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		entity, err := url.ParseQuery(string(bytes))
		if err != nil {
			return nil, err
		}
		return entity, nil
	case "multipart/form-data":
		boundary, ok := params["boundary"]
		if !ok {
			return nil, http.ErrMissingBoundary
		}
		reader := multipart.NewReader(r, boundary)
		entity, err := reader.ReadForm(0)
		if err != nil {
			return nil, err
		}
		return entity, nil
	case "application/json":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		var entity interface{}
		err = json.Unmarshal(bytes, &entity)
		if err != nil {
			return nil, err
		}
		return entity, nil
	case "application/octet-stream":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		return bytes, nil
	}
	return nil, nil
}
<commit_msg>httpentity: have multipart/form-data parse into the expected format<commit_after>// Copyright 2015 Luke Shumaker

package httpentity

import (
	"encoding/json"
	"io"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/url"
	"net/http"
)

func (r Response) WriteEntity(w io.Writer) error {
	if r.entity == nil {
		return nil
	}
	mimetype, _, _ := mime.ParseMediaType(r.Headers.Get("Content-Type"))
	encoders := r.entity.Encoders()
	return encoders[mimetype](w)
}

func ReadEntity(r io.Reader, contenttype string) (interface{}, error) {
	mimetype, params, err := mime.ParseMediaType(contenttype)
	if err != nil {
		return nil, err
	}
	switch mimetype {
	case "application/x-www-form-urlencoded":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		entity, err := url.ParseQuery(string(bytes))
		if err != nil {
			return nil, err
		}
		return entity, nil
	case "multipart/form-data":
		boundary, ok := params["boundary"]
		if !ok {
			return nil, http.ErrMissingBoundary
		}
		reader := multipart.NewReader(r, boundary)
		form, err := reader.ReadForm(0)
		if err != nil {
			return nil, err
		}
		entity := make(map[string]interface{}, len(form.Value)+len(form.File))
		for k, v := range form.Value {
			entity[k] = v
		}
		for k, v := range form.File {
			if _, exists := entity[k]; exists {
				values := entity[k].([]string)
				entity[k] = make([]interface{}, len(values)+len(v))
				i := uint(0)
				for _, value := range values {
					entity[k][i] = value
					i++
				}
				for _, value := range v {
					entity[k][i] = value
					i+++
				}
			} else {
				entity[k] = v
			}
		}
		return entity, nil
	case "application/json":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		var entity interface{}
		err = json.Unmarshal(bytes, &entity)
		if err != nil {
			return nil, err
		}
		return entity, nil
	case "application/octet-stream":
		bytes, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}
		return bytes, nil
	}
	return nil, nil
}
<|endoftext|>