Ejemplo n.º 1
0
func setDefaults(log logging.Logger) {
	group, err := modelhelper.GetGroup(models.Channel_KODING_NAME)
	if err != nil {
		log.Error("err while fetching koding group: %s", err.Error())
		return
	}

	log.Debug("mongo group found")

	setPublicChannel(log, group)
	setChangeLogChannel(log, group)
	log.Info("socialApi defaults are created")
}
Ejemplo n.º 2
0
// Join copies data between local and remote connections.
// It reads from one connection and writes to the other.
// It's a building block for ProxyFunc implementations.
func Join(local, remote net.Conn, log logging.Logger) {
	var wg sync.WaitGroup
	wg.Add(2)

	transfer := func(side string, dst, src net.Conn) {
		log.Debug("proxing %s -> %s", src.RemoteAddr(), dst.RemoteAddr())

		n, err := io.Copy(dst, src)
		if err != nil {
			log.Error("%s: copy error: %s", side, err)
		}

		if err := src.Close(); err != nil {
			log.Debug("%s: close error: %s", side, err)
		}

		// not for yamux streams, but for client to local server connections
		if d, ok := dst.(*net.TCPConn); ok {
			if err := d.CloseWrite(); err != nil {
				log.Debug("%s: closeWrite error: %s", side, err)
			}

		}
		wg.Done()
		log.Debug("done proxing %s -> %s: %d bytes", src.RemoteAddr(), dst.RemoteAddr(), n)
	}

	go transfer("remote to local", local, remote)
	go transfer("local to remote", remote, local)

	wg.Wait()
}
Ejemplo n.º 3
0
// createHostedZone creates hosted zone and makes sure that it is in to be used
// state
func (r *RecordManager) createHostedZone(hostedZoneLogger logging.Logger) error {
	hostedZoneLogger.Debug("create hosted zone started")

	// CreateHostedZone is not idempotent, multiple calls to this function
	// result in duplicate records, fyi
	resp, err := r.route53.CreateHostedZone(&route53.CreateHostedZoneInput{
		CallerReference: aws.String(r.hostedZoneConf.CallerReference),
		Name:            aws.String(r.hostedZoneConf.Name),
		HostedZoneConfig: &route53.HostedZoneConfig{
			Comment: aws.String(hostedZoneComment),
		},
	})
	if err != nil {
		return err
	}

	if resp == nil || resp.ChangeInfo == nil {
		return errors.New("malformed response, resp is nil")
	}

	deadline := time.After(time.Minute * 5) // at most i've seen ~3min
	check := time.NewTicker(time.Second * 3)

	for {
		select {
		case <-deadline:
			return errDeadlineReachedForChangeInfo
		case <-check.C:
			hostedZoneLogger.New("changeInfoID", *resp.ChangeInfo.Id).Debug("fetching latest status")
			getChangeResp, err := r.route53.GetChange(&route53.GetChangeInput{
				Id: resp.ChangeInfo.Id,
			})
			if err != nil {
				return err
			}

			if getChangeResp == nil || getChangeResp.ChangeInfo == nil {
				return errors.New("malformed response, getChangeResp is nil")
			}

			if getChangeResp.ChangeInfo.Status != nil && *getChangeResp.ChangeInfo.Status == validStateForHostedZone {
				r.hostedZone = resp.HostedZone
				hostedZoneLogger.Debug("create hosted finished successfully")
				return nil
			}
		}
	}
}
Ejemplo n.º 4
0
func setChangeLogChannel(log logging.Logger, group *kodingmodels.Group) {

	c := models.NewChannel()
	selector := map[string]interface{}{
		"type_constant": models.Channel_TYPE_ANNOUNCEMENT,
		"group_name":    models.Channel_KODING_NAME,
	}

	// if err is nil
	// it means we already have that channel
	err := c.One(bongo.NewQS(selector))
	if err != nil && err != bongo.RecordNotFound {
		log.Error("err while fetching changelog channel:", err.Error())
		return
	}

	if err == bongo.RecordNotFound {
		log.Error("postgres changelog couldn't found, creating it")

		acc, err := createChannelOwner(group)
		if err != nil {
			log.Error(err.Error())
			return
		}

		c.Name = "changelog"
		c.CreatorId = acc.Id
		c.GroupName = models.Channel_KODING_NAME
		c.TypeConstant = models.Channel_TYPE_ANNOUNCEMENT
		c.PrivacyConstant = models.Channel_PRIVACY_PRIVATE
		if err := c.Create(); err != nil {
			log.Error("err while creating the koding channel:", err.Error())
			return
		}
	}

	socialApiAnnouncementChannelId := strconv.FormatInt(c.Id, 10)
	if group.SocialApiAnnouncementChannelId == socialApiAnnouncementChannelId {
		log.Info("mongo and postgres socialApiAnnouncementChannel ids are same")
		return
	}

	log.Debug("mongo and postgres socialApiAnnouncementChannel ids are different, fixing it")
	if err := updateGroupPartially(group.Id, "socialApiAnnouncementChannelId", strconv.FormatInt(c.Id, 10)); err != nil {
		log.Error("err while udpating socialApiAnnouncementChannelId:", err.Error())
		return
	}
}
Ejemplo n.º 5
0
// provisionData creates the base64-json-encoded userdata.Value to be sent
// altogether with create request.
func (cmd *VagrantCreate) provisionData(log logging.Logger) (string, error) {
	kiteID := uuid.NewV4().String()

	keycreator := &keycreator.Key{
		KontrolURL:        cmd.KontrolURL,
		KontrolPrivateKey: defaultPrivateKey,
		KontrolPublicKey:  defaultPublicKey,
	}

	kiteKey, err := keycreator.Create(cmd.Username, kiteID)
	if err != nil {
		return "", err
	}

	data := &puser.Value{
		Username:        cmd.Username,
		Groups:          []string{"sudo"},
		Hostname:        cmd.Username,
		KiteKey:         kiteKey,
		LatestKlientURL: cmd.KlientURL,
		RegisterURL:     cmd.RegisterURL,
		KontrolURL:      cmd.KontrolURL,
	}

	log.Debug("provision data: %+v", data)

	p, err := json.Marshal(data)
	if err != nil {
		return "", err
	}

	return base64.StdEncoding.EncodeToString(p), nil
}
Ejemplo n.º 6
0
// NewBadRequestWithLogger is creating a new http response with predifined http
// response properties, it uses a special logger for outputting callstack
// properly
func NewBadRequestWithLogger(l logging.Logger, err error) (int, http.Header, interface{}, error) {
	if err == nil {
		err = errors.New("request is not valid")
	}

	// make sure errors are outputted
	l.Debug("Bad Request: %s", err)

	// do not expose errors to the client
	env := config.MustGet().Environment

	// do not expose errors to the client.
	if env != "dev" && env != "test" && socialApiEnv != "wercker" {
		err = genericError
	}

	return http.StatusBadRequest, nil, nil, BadRequest{err}
}
Ejemplo n.º 7
0
// startIntervalerIfNeeded starts the given rsync interval, logs any errors, and adds the
// resulting Intervaler to the Mount struct for later Stoppage.
func startIntervalerIfNeeded(log logging.Logger, remoteMachine *machine.Machine, c *rsync.Client, opts rsync.SyncIntervalOpts) {
	log = log.New("startIntervalerIfNeeded")

	if opts.Interval <= 0 {
		// Using debug, because this is not an error - just informative.
		log.Debug(
			"startIntervalerIfNeeded() called with interval:%d. Cannot start Intervaler",
			opts.Interval,
		)
		return
	}

	log.Info("Creating and starting RSync SyncInterval")
	intervaler, err := c.SyncInterval(opts)
	if err != nil {
		log.Error("rsync SyncInterval returned an error:%s", err)
		return
	}

	remoteMachine.Intervaler = intervaler
}
Ejemplo n.º 8
0
func (l *LifeCycle) createQueue(sqsLogger logging.Logger, queueName string) error {
	if l.sqs == nil {
		return errSQSNotSet
	}

	// CreateQueue is idempotent, if it is already created returns existing one
	// all Attributes should be same with consecutive calls
	createQueueResp, err := l.sqs.CreateQueue(
		&sqs.CreateQueueInput{
			QueueName:  aws.String(queueName), // Required
			Attributes: attributes,
		},
	)
	if err != nil {
		return err
	}

	l.queueURL = createQueueResp.QueueUrl // dont forget to assign queue url

	sqsLogger.Debug("SQS Queue is created")
	return nil
}
Ejemplo n.º 9
0
func (l *LifeCycle) configureQueue(sqsLogger logging.Logger) error {
	// get attributes of queue
	resp, err := l.getQueueAttributes()
	if err != nil {
		return err
	}

	// create our custom policy for access management
	b, err := newDefaultPolicy(*l.topicARN, *l.queueARN)
	if err != nil {
		return err
	}

	// Attributes: {
	//   QueueArn: "arn:aws:sqs:us-east-1:616271189586:SQS-ElasticBeanstalkNotifications-Environment-cihangir",
	//   Policy: "{\"Version\":\"2012-10-17\",\"Id\":\"arn:aws:sqs:us-east-1:616271189586:SQS-ElasticBeanstalkNotifications-Environment-cihangir/SQSDefaultPolicy\",\"Statement\":[{\"Sid\":\"tunnelproxy_dev_1\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:us-east-1:616271189586:SQS-ElasticBeanstalkNotifications-Environment-cihangir\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:us-east-1:616271189586:tunnelproxymanager_test\"}}}]}"
	// }
	// check if current policy is valid
	if isPolicyValid(resp.Attributes, b) {
		return nil
	}

	sqsLogger.Debug("Queue Policy is not correct, fixing it...")

	_, err = l.sqs.SetQueueAttributes(&sqs.SetQueueAttributesInput{
		Attributes: map[string]*string{
			"Policy": aws.String(b),
		},
		QueueUrl: l.queueURL,
	})
	if err != nil {
		return err
	}

	sqsLogger.Debug("Queue Policy is configured properly")
	return nil
}
Ejemplo n.º 10
0
func setPublicChannel(log logging.Logger, group *kodingmodels.Group) {
	c := models.NewChannel()
	selector := map[string]interface{}{
		"type_constant": models.Channel_TYPE_GROUP,
		"group_name":    models.Channel_KODING_NAME,
	}

	err := c.One(bongo.NewQS(selector))
	if err != nil && err != bongo.RecordNotFound {
		log.Error("err while fetching koding channel:", err.Error())
		return
	}

	if err == bongo.RecordNotFound {
		log.Debug("postgres group couldn't found, creating it")

		acc, err := createChannelOwner(group)
		if err != nil {
			log.Error(err.Error())
			return
		}

		c.Name = "public"
		c.CreatorId = acc.Id
		c.GroupName = models.Channel_KODING_NAME
		c.TypeConstant = models.Channel_TYPE_GROUP
		c.PrivacyConstant = models.Channel_PRIVACY_PUBLIC
		if err := c.Create(); err != nil {
			log.Error("err while creating the koding channel: %s", err.Error())
			return
		}
	}

	socialApiId := strconv.FormatInt(c.Id, 10)
	if group.SocialApiChannelId == socialApiId {
		log.Debug("mongo and postgres socialApiChannelId ids are same")
		return
	}

	log.Debug("mongo and postgres socialApiChannelId ids are different, fixing it")
	if err := updateGroupPartially(group.Id, "socialApiChannelId", socialApiId); err != nil {
		log.Error("err while udpating socialApiChannelId: %s", err.Error())
		return
	}
}
Ejemplo n.º 11
0
func AuthLogin(c *cli.Context, log logging.Logger, _ string) (int, error) {
	kodingURL, err := url.Parse(c.String("baseurl"))
	if err != nil {
		return 1, fmt.Errorf("%q is not a valid URL value: %s\n", c.String("koding"), err)
	}

	k, ok := configstore.List()[config.ID(kodingURL.String())]
	if !ok {
		k = &config.Konfig{
			Endpoints: &config.Endpoints{
				Koding: config.NewEndpointURL(kodingURL),
			},
		}
	}

	if err := configstore.Use(k); err != nil {
		return 1, err
	}

	// We create here a kloud client instead of using kloud.DefaultClient
	// in order to handle first-time login attempts where configuration
	// for kloud does not yet exist.
	kloudClient := &kloud.Client{
		Transport: &kloud.KiteTransport{
			Konfig: k,
			Log:    log,
		},
	}

	testKloudHook(kloudClient)

	authClient := &auth.Client{
		Kloud: kloudClient,
	}

	teamClient := &team.Client{
		Kloud: kloudClient,
	}

	// If we already own a valid kite.key, it means we were already
	// authenticated and we just call kloud using kite.key authentication.
	err = kloudClient.Transport.(stack.Validator).Valid()

	log.Debug("auth: transport test: %s", err)

	opts := &auth.LoginOptions{
		Team: c.String("team"),
	}

	if err != nil {
		opts.Username, err = helper.Ask("Username [%s]: ", config.CurrentUser.Username)
		if err != nil {
			return 1, err
		}

		if opts.Username == "" {
			opts.Username = config.CurrentUser.Username
		}

		for {
			opts.Password, err = helper.AskSecret("Password [***]: ")
			if err != nil {
				return 1, err
			}
			if opts.Password != "" {
				break
			}
		}
	}

	fmt.Fprintln(os.Stderr, "Logging to", kodingURL, "...")

	resp, err := authClient.Login(opts)
	if err != nil {
		return 1, fmt.Errorf("error logging into your Koding account: %v", err)
	}

	if resp.KiteKey != "" {
		k.KiteKey = resp.KiteKey
		k.Endpoints = resp.Metadata.Endpoints

		if err := configstore.Use(k); err != nil {
			return 1, err
		}
	}

	teamClient.Use(&team.Team{Name: resp.GroupName})

	if c.Bool("json") {
		enc := json.NewEncoder(os.Stdout)
		enc.SetIndent("", "\t")
		enc.Encode(resp)
	} else {
		fmt.Fprintln(os.Stdout, "Successfully logged in to the following team:", resp.GroupName)
	}

	return 0, nil
}