Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the remoteConfigFactory interface public #1144

Closed
idlebot opened this issue May 28, 2021 · 3 comments
Closed

Make the remoteConfigFactory interface public #1144

idlebot opened this issue May 28, 2021 · 3 comments
Labels
resolution/invalid This doesn't seem right

Comments

@idlebot
Copy link

idlebot commented May 28, 2021

While I understand that #1046 is the better solution, making the remoteConfigFactory interface public would allow getting configuration values from a different source.

In particular, I would like to implement a way to read values from Google Cloud Secret Manager.

By making this interface public it would be possible to simply do something like:

package vipergcpsecretmanager

import (
	"bytes"
	"context"
	"fmt"
	"io"

	secretmanager "cloud.google.com/go/secretmanager/apiv1"
	"github.com/spf13/viper"
	secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)

type SecretManagerConfigProvider struct {
	projectID string
}

func (p SecretManagerConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
	ctx := context.Background()
	client, err := secretmanager.NewClient(ctx)
	if err != nil {
		return nil, err
	}

	// Build the request.
	req := &secretmanagerpb.AccessSecretVersionRequest{
		Name: fmt.Sprintf("projects/%s/secrets/%s/versions/latest", p.projectID, rp.Path()),
	}

	// Call the API.
	result, err := client.AccessSecretVersion(ctx, req)
	if err != nil {
		return nil, err
	}

	return bytes.NewReader(result.Payload.Data), nil
}

func (p SecretManagerConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
	return bytes.NewReader([]byte{}), nil
}

func (p SecretManagerConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
	quitwc := make(chan bool)
	viperResponsCh := make(chan *viper.RemoteResponse)

	return viperResponsCh, quitwc
}

And set the factory from user code:

viper.RemoteConfig = &SecretManagerConfigProvider{
	projectID: "my-project-id-123"
}
@idlebot idlebot added the kind/enhancement New feature or request label May 28, 2021
@github-actions
Copy link

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news,
either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️

@sagikazarmark
Copy link
Collaborator

You don't need the remoteConfigFactory interface to be public, in order to implement it. Your code should work as it is.

Here is an example using hashicorp vault as a remote backend: https://github.com/sagikazarmark/viperx

@idlebot
Copy link
Author

idlebot commented May 28, 2021

Thanks!

@idlebot idlebot closed this as completed May 28, 2021
@sagikazarmark sagikazarmark added resolution/invalid This doesn't seem right and removed kind/enhancement New feature or request labels May 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution/invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants