Example #1
0
func authenticateFromSessionID(r *kite.Request) error {
	username, err := findUsernameFromSessionID(r.Auth.Key)
	if err != nil {
		return err
	}

	r.Username = username

	return nil
}
Example #2
0
// stackMethod routes the team method call to a requested provider.
func (k *Kloud) stackMethod(r *kite.Request, fn StackFunc) (interface{}, error) {
	if r.Args == nil {
		return nil, NewError(ErrNoArguments)
	}

	var args TeamRequest

	// Unamrshal common arguments.
	if err := r.Args.One().Unmarshal(&args); err != nil {
		return nil, errors.New("invalid request: " + err.Error())
	}

	// TODO(rjeczalik): compatibility code, remove
	if args.Provider == "" {
		args.Provider = "aws"
	}

	if IsKloudctlAuth(r, k.SecretKey) {
		// kloudctl is not authenticated with username, let it overwrite it
		r.Username = args.Impersonate
	}

	k.Log.Debug("Called %q by %q with %q", r.Method, r.Username, r.Args.Raw)

	if args.GroupName == "" {
		args.GroupName = "koding"
	}

	s, ctx, err := k.newStack(r, &args)
	if err != nil {
		return nil, err
	}

	ctx = k.traceRequest(ctx, args.metricTags())

	// Currently only apply method is asynchronous, rest
	// of the is sync. That's why the fn execution is synchronous here,
	// and the fn itself emits events if needed.
	//
	// This differs from k.coreMethods.
	resp, err := fn(s, ctx)

	// Do not log error in production as most of them are expected:
	//
	//  - plan errors due to invalud user input
	if err != nil {
		if _, ok := err.(*Error); ok {
			k.Log.Warning("%s (method=%s, user=%s, group=%s)", err, r.Method, r.Username, args.GroupName)
		} else {
			k.Log.Debug("method %q for user %q failed: %s", r.Method, r.Username, err)
		}

		// ensure UI receives proper error origin - kloudError
		if _, ok := err.(*kite.Error); !ok {
			err = NewErrorMessage(err.Error())
		}
	}

	k.send(ctx)

	return resp, err
}