예제 #1
0
// processFlags validates flags, creates and configures logger, client, etc.
func (opts *commonOptions) processFlags() (state, error) {
	// Logger.
	logLevels := map[logging.Level]gol.Level{
		logging.Debug:   gol.DEBUG,
		logging.Info:    gol.INFO,
		logging.Warning: gol.WARNING,
		logging.Error:   gol.ERROR,
	}
	logger := gologger.New(os.Stderr, logLevels[opts.localLogLevel])

	// Auth options.
	authOpts, err := opts.authFlags.Options()
	if err != nil {
		return state{}, err
	}
	authOpts.Logger = logger
	if opts.projectID == "" {
		if authOpts.ServiceAccountJSONPath != "" {
			opts.projectID = projectIDFromServiceAccountJSON(authOpts.ServiceAccountJSONPath)
		}
		if opts.projectID == "" {
			return state{}, fmt.Errorf("-project-id is required")
		}
	}

	// Client.
	httpClient, err := auth.AuthenticatedClient(auth.SilentLogin, auth.NewAuthenticator(authOpts))
	if err != nil {
		return state{}, err
	}
	client, err := cloudtail.NewClient(cloudtail.ClientOptions{
		Client:       httpClient,
		Logger:       logger,
		ProjectID:    opts.projectID,
		ResourceType: opts.resourceType,
		ResourceID:   opts.resourceID,
		LogID:        opts.logID,
	})
	if err != nil {
		return state{}, err
	}

	// Buffer.
	buffer := cloudtail.NewPushBuffer(cloudtail.PushBufferOptions{
		Client: client,
		Logger: logger,
	})

	return state{logger, client, buffer}, nil
}
예제 #2
0
func (opts *ServiceOptions) makeCipdClient(root string) (cipd.Client, error) {
	authOpts, err := opts.authFlags.Options()
	if err != nil {
		return nil, err
	}
	return cipd.NewClient(cipd.ClientOptions{
		ServiceURL: opts.serviceURL,
		Root:       root,
		Logger:     log,
		AuthenticatedClientFactory: func() (*http.Client, error) {
			return auth.AuthenticatedClient(auth.OptionalLogin, auth.NewAuthenticator(authOpts))
		},
	}), nil
}
예제 #3
0
func (c *loginRun) Run(subcommands.Application, []string) int {
	opts, err := c.flags.Options()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		return 1
	}
	client, err := auth.AuthenticatedClient(auth.InteractiveLogin, auth.NewAuthenticator(opts))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Login failed: %s\n", err.Error())
		return 2
	}
	err = reportIdentity(client)
	if err != nil {
		return 3
	}
	return 0
}
예제 #4
0
func (c *infoRun) Run(a subcommands.Application, args []string) int {
	opts, err := c.flags.Options()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.Error())
		return 1
	}
	client, err := auth.AuthenticatedClient(auth.SilentLogin, auth.NewAuthenticator(opts))
	if err == auth.ErrLoginRequired {
		fmt.Fprintln(os.Stderr, "Not logged in")
		return 2
	} else if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 3
	}
	err = reportIdentity(client)
	if err != nil {
		return 4
	}
	return 0
}
예제 #5
0
func (c *config) createAuthenticatedClient(ctx context.Context) (*http.Client, error) {
	scopes := []string{}
	scopes = append(scopes, cloudLoggingScopes...)
	scopes = append(scopes, pubsubScopes...)

	// Get our authenticated client.
	options := auth.Options{
		Scopes:                 scopes,
		ServiceAccountJSONPath: c.serviceAccountJSONPath,
		Logger:                 log.Get(ctx),
	}
	authenticator := auth.NewAuthenticator(options)

	mode := auth.SilentLogin
	if c.authInteractive {
		mode = auth.InteractiveLogin
	}

	return auth.AuthenticatedClient(mode, authenticator)
}
예제 #6
0
func (c *endpointConfig) createService(ctx context.Context) (endpointService, error) {
	if c.url == "" {
		return nil, errors.New("endpoint: you must supply a monitoring endpoint")
	}

	authenticator := auth.NewAuthenticator(
		auth.Options{
			Method:                 auth.ServiceAccountMethod,
			Scopes:                 endpointScopes,
			ServiceAccountJSONPath: c.serviceAccountJSONPath,
			Logger:                 log.Get(ctx),
		})
	client, err := auth.AuthenticatedClient(auth.SilentLogin, authenticator)
	if err != nil {
		log.Errorf(log.SetError(ctx, err), "Failed to configure endpoint client.")
		return nil, errors.New("endpoint: failed to configure endpoint client")
	}

	return &endpointServiceImpl{
		endpointConfig: *c,
		client:         client,
	}, nil
}