Пример #1
0
func configure(logger lager.Logger) (*ssh.ServerConfig, error) {
	cf_http.Initialize(*communicationTimeout)

	if *diegoAPIURL == "" {
		err := errors.New("diegoAPIURL is required")
		logger.Fatal("diego-api-url-required", err)
	}

	url, err := url.Parse(*diegoAPIURL)
	if err != nil {
		logger.Fatal("failed-to-parse-diego-api-url", err)
	}

	_, err = url.Parse(*ccAPIURL)
	if *ccAPIURL != "" && err != nil {
		logger.Fatal("failed-to-parse-cc-api-url", err)
	}

	var diegoCreds string
	if url.User != nil {
		diegoCreds = url.User.String()
	}

	receptorClient := receptor.NewClient(*diegoAPIURL)

	authenticatorMap := map[string]authenticators.PasswordAuthenticator{}

	if *enableDiegoAuth {
		diegoAuthenticator := authenticators.NewDiegoProxyAuthenticator(logger, receptorClient, []byte(diegoCreds))
		authenticatorMap[diegoAuthenticator.Realm()] = diegoAuthenticator
	}

	if *ccAPIURL != "" && *enableCFAuth {
		ccClient := cf_http.NewClient()
		cfAuthenticator := authenticators.NewCFAuthenticator(logger, ccClient, *ccAPIURL, receptorClient)
		authenticatorMap[cfAuthenticator.Realm()] = cfAuthenticator
	}

	authenticator := authenticators.NewCompositeAuthenticator(authenticatorMap)

	sshConfig := &ssh.ServerConfig{
		PasswordCallback: authenticator.Authenticate,
		AuthLogCallback: func(cmd ssh.ConnMetadata, method string, err error) {
			logger.Error("authentication-failed", err, lager.Data{"user": cmd.User()})
		},
	}

	if *hostKey == "" {
		err := errors.New("hostKey is required")
		logger.Fatal("host-key-required", err)
	}

	key, err := parsePrivateKey(logger, *hostKey)
	if err != nil {
		logger.Fatal("failed-to-parse-host-key", err)
	}

	sshConfig.AddHostKey(key)

	return sshConfig, err
}
	Describe("Authenticate", func() {
		var (
			authenticator    *authenticators.CompositeAuthenticator
			authenticatorMap map[string]authenticators.PasswordAuthenticator
			metadata         *fake_ssh.FakeConnMetadata
			password         []byte
		)

		BeforeEach(func() {
			authenticatorMap = map[string]authenticators.PasswordAuthenticator{}
			metadata = &fake_ssh.FakeConnMetadata{}
			password = []byte{}
		})

		JustBeforeEach(func() {
			authenticator = authenticators.NewCompositeAuthenticator(authenticatorMap)
		})

		Context("when no authenticators are specified", func() {
			It("fails to authenticate", func() {
				_, err := authenticator.Authenticate(metadata, password)
				Expect(err).To(Equal(authenticators.InvalidCredentialsErr))
			})
		})

		Context("when one or more authenticators are specified", func() {
			var (
				authenticatorOne *fake_authenticators.FakePasswordAuthenticator
				authenticatorTwo *fake_authenticators.FakePasswordAuthenticator
			)
Пример #3
0
func configureProxy(logger lager.Logger) (*ssh.ServerConfig, error) {
	if *bbsAddress == "" {
		err := errors.New("bbsAddress is required")
		logger.Fatal("bbs-address-required", err)
	}

	url, err := url.Parse(*bbsAddress)
	if err != nil {
		logger.Fatal("failed-to-parse-bbs-address", err)
	}

	bbsClient := initializeBBSClient(logger)
	permissionsBuilder := authenticators.NewPermissionsBuiler(bbsClient)

	authens := []authenticators.PasswordAuthenticator{}

	if *enableDiegoAuth {
		diegoAuthenticator := authenticators.NewDiegoProxyAuthenticator(logger, []byte(*diegoCredentials), permissionsBuilder)
		authens = append(authens, diegoAuthenticator)
	}

	if *enableCFAuth {
		if *ccAPIURL == "" {
			return nil, errors.New("ccAPIURL is required for Cloud Foundry authentication")
		}

		_, err = url.Parse(*ccAPIURL)
		if *ccAPIURL != "" && err != nil {
			return nil, err
		}

		if *uaaPassword == "" {
			return nil, errors.New("UAA password is required for Cloud Foundry authentication")
		}

		if *uaaUsername == "" {
			return nil, errors.New("UAA username is required for Cloud Foundry authentication")
		}

		if *uaaTokenURL == "" {
			return nil, errors.New("uaaTokenURL is required for Cloud Foundry authentication")
		}

		_, err = url.Parse(*uaaTokenURL)
		if *uaaTokenURL != "" && err != nil {
			return nil, err
		}

		client := NewHttpClient()
		cfAuthenticator := authenticators.NewCFAuthenticator(
			logger,
			client,
			*ccAPIURL,
			*uaaTokenURL,
			*uaaUsername,
			*uaaPassword,
			permissionsBuilder,
		)
		authens = append(authens, cfAuthenticator)
	}

	authenticator := authenticators.NewCompositeAuthenticator(authens...)

	sshConfig := &ssh.ServerConfig{
		PasswordCallback: authenticator.Authenticate,
		AuthLogCallback: func(cmd ssh.ConnMetadata, method string, err error) {
			logger.Error("authentication-failed", err, lager.Data{"user": cmd.User()})
		},
	}

	if *hostKey == "" {
		err := errors.New("hostKey is required")
		logger.Fatal("host-key-required", err)
	}

	key, err := parsePrivateKey(logger, *hostKey)
	if err != nil {
		logger.Fatal("failed-to-parse-host-key", err)
	}

	sshConfig.AddHostKey(key)

	return sshConfig, err
}
	Describe("Authenticate", func() {
		var (
			authenticator *authenticators.CompositeAuthenticator
			authens       []authenticators.PasswordAuthenticator
			metadata      *fake_ssh.FakeConnMetadata
			password      []byte
		)

		BeforeEach(func() {
			authens = []authenticators.PasswordAuthenticator{}
			metadata = &fake_ssh.FakeConnMetadata{}
			password = []byte{}
		})

		JustBeforeEach(func() {
			authenticator = authenticators.NewCompositeAuthenticator(authens...)
		})

		Context("when no authenticators are specified", func() {
			It("fails to authenticate", func() {
				_, err := authenticator.Authenticate(metadata, password)
				Expect(err).To(Equal(authenticators.InvalidCredentialsErr))
			})
		})

		Context("when one or more authenticators are specified", func() {
			var (
				authenticatorOne *fake_authenticators.FakePasswordAuthenticator
				authenticatorTwo *fake_authenticators.FakePasswordAuthenticator
			)