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

Add support for connection.update-secret #114

Merged
merged 2 commits into from Sep 6, 2022

Conversation

Zerpet
Copy link
Contributor

@Zerpet Zerpet commented Sep 1, 2022

This method is in the AMQP 0.9.1 spec and it is supported by RabbitMQ.
This is useful when the secret used in the connection has an expiry, as
it allows the applications to renew the secret without the hassle of
closing-reconnecting consumers/producers.

This method is in the AMQP 0.9.1 spec and it is supported by RabbitMQ.
This is useful when the secret used in the connection has an expiry, as
it allows the applications to renew the secret without the hassle of
closing-reconnecting consumers/producers.

Signed-off-by: Aitor Perez Cedres <acedres@vmware.com>
@Zerpet Zerpet linked an issue Sep 1, 2022 that may be closed by this pull request
@Zerpet Zerpet added this to the 1.5.0 milestone Sep 1, 2022
Setting up an OAuth2 server to end-2-end test this function feels a bit
overkill. This function is used mainly in the context of OAuth2
authentication, where tokens expire. From this client perspective, all
it has to ensure is that a specific frame is put out in the wire to
RabbitMQ server. Then we trust that RabbitMQ will hold its part of the
contract and do the right thing.

Signed-off-by: Aitor Perez Cedres <acedres@vmware.com>
@Zerpet Zerpet marked this pull request as ready for review September 5, 2022 16:42
@Gsantomaggio
Copy link
Member

Looks to me.
I tested it with https://github.com/rabbitmq/rabbitmq-oauth2-tutorial

package main

import (
	"context"
	"encoding/json"
	"fmt"
	amqp "github.com/rabbitmq/amqp091-go"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

func main() {
	fmt.Println(GetToken())

	connection, err := amqp.DialConfig(fmt.Sprintf(
		"%s://%s:%s@%s:%s/%s",
		"amqp",
		"",
		GetToken(),
		"localhost",
		"5672",
		"/",
	), amqp.Config{})

	if err != nil {
		panic(err)
	}
	channel, err := connection.Channel()
	if err != nil {
		panic(err)
	}
	for i := 0; i < 1000; i++ {

		err := channel.PublishWithContext(context.Background(), "", "test-key", false, false, amqp.Publishing{})
		if err != nil {
			fmt.Printf("Error %s\n", err)
			return
		}

		if i%59 == 0 {
			err := connection.UpdateSecret(GetToken(), "test-key")
			fmt.Printf("Update secret\n")
			if err != nil {
				fmt.Printf("UpdateSecret Error %s\n", err)

				return
			}
		}

		time.Sleep(1 * time.Second)
		fmt.Printf("Sent %d\n", i)
	}
	defer connection.Close()

}

type Access struct {
	AccessToken string `json:"access_token"`
}

func GetToken() string {
	url := "http://localhost:8080/realms/test/protocol/openid-connect/token"
	method := "POST"

	payload := strings.NewReader("client_id=producer&client_secret=kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn&grant_type=client_credentials")

	client := &http.Client{}
	req, err := http.NewRequest(method, url, payload)

	if err != nil {
		fmt.Println(err)
		return ""
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return ""
	}

	var x = Access{}
	err = json.Unmarshal(body, &x)
	if err != nil {
		return err.Error()
	}
	return x.AccessToken
}

@Gsantomaggio Gsantomaggio merged commit ac70118 into main Sep 6, 2022
@Gsantomaggio Gsantomaggio deleted the 107-support-connectionupdate-secret branch September 6, 2022 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support connection.update-secret
2 participants