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

[broker] Fix topic produced through REST not support Authorization #13771

Conversation

liudezhi2098
Copy link
Contributor

Master Issue: #13766

Motivation

fix topic produced through REST not support Authorization

Documentation

  • no-need-doc

@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Jan 16, 2022
@mattisonchao
Copy link
Member

Hi , @liudezhi2098

Could you add some test for this change ?

@liudezhi2098
Copy link
Contributor Author

/pulsarbot run-failure-checks

@liudezhi2098
Copy link
Contributor Author

/pulsarbot run-failure-checks

2 similar comments
@liudezhi2098
Copy link
Contributor Author

/pulsarbot run-failure-checks

@liudezhi2098
Copy link
Contributor Author

/pulsarbot run-failure-checks

@thomasechen
Copy link

Hi @codelipenghui , The fix is critical to the existing community users who want to use the REST function. I suggest to add the fix as part of the release 2.9.2.

@thomasechen
Copy link

Hi @codelipenghui , The fix is critical to the existing community users who want to use the REST function. I suggest to add the fix as part of the release 2.9.2.

Sorry , I mean the users that already implemented the Pulsar in their environment

@codelipenghui
Copy link
Contributor

@thomasechen Yes, we can contain it in 2.9.2

Copy link
Member

@Demogorgon314 Demogorgon314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, just left a few comments about code style.

public void testProduceToNonPartitionedTopic(String token, int status) throws Exception {
admin.topics().createNonPartitionedTopic("persistent://" + testTenant + "/"
+ testNamespace + "/" + testTopicName);
AsyncResponse asyncResponse = PowerMockito.mock(AsyncResponse.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The asyncResponse is never used, we can remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Others asyncResponse can be removed either.

Comment on lines 95 to 96
topics = spy(new Topics());
topics.setPulsar(pulsar);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topics never used, we can remove it.

Comment on lines 125 to 145
admin.topics().createNonPartitionedTopic("persistent://" + testTenant + "/"
+ testNamespace + "/" + testTopicName);
Schema<String> schema = StringSchema.utf8();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().
writeValueAsString(schema.getSchemaInfo()));
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().
writeValueAsString(schema.getSchemaInfo()));
String message = "[" +
"{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," +
"{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message,
new TypeReference<List<ProducerMessage>>() {
}));

WebTarget root = buildWebClient();
Response response = root.path("/topics/persistent/" + testTenant + "/" + testNamespace + "/"
+ testTopicName).request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + token)
.post(Entity.json(producerMessages));
Assert.assertEquals(response.getStatus(), status);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some repeated code blocks, maybe we could try to reuse the same code block. It seems that only topic names are different.

@liudezhi2098
Copy link
Contributor Author

/pulsarbot run-failure-checks

@codelipenghui codelipenghui merged commit f551fdc into apache:master Jan 20, 2022
codelipenghui pushed a commit that referenced this pull request Jan 21, 2022
@codelipenghui codelipenghui added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label Jan 21, 2022
@thomasechen
Copy link

thomasechen commented Jan 25, 2022

@liudezhi2098

I have done the testing about producing the message through REST client. The HTTP client could produce the message successfully if I granted the authorization in the namespace or topic level. However, if I use the token which belongs to the tenant admin and there are no any permissions settings in the namespace and the topics level ,the system will refuse the HTTP client to produce the message to the topic and show the message under below.

"Unauthorized to produce to topic persistent://tls/demo4/test with clientAppId [tls] and authdata org.apache.pulsar.broker.authentication.AuthenticationDataHttps@318c6550"}"

Actually, if we use the client library (Java, Node), we can produce the message to the topic using the tenant admin token (the tenant admin role was set ) without granting additional permissions to the namespace or topics.

Reproduce :

  1. I have a tenant called tls which admin role is also called "tls"
    image

  2. There is no permission settings related to the namespace tls/demo4
    image

  3. We use the token of the role "tls" to produce a message and succeed
    image
    image

  4. We use "POSTMAN" to produce a message to the same topic
    4.1 Authorization Settings: The token is the role "tls'
    image

    4.2 Message Body Detail
    image

  5. The system will give me the unauthorized message
    image

  6. After we grant the permission allowing producing message to the namespace tls/demo4, then we can successfully produce the message
    6.1 Grant the permission to the role tls
    image

    6.2 Produce the message successfully
    image

Conclusion:
REST authorization mechanism should be as same as the other clients (JAVA, NODEJS ......), We should be able to produce the message to the topic with the tenant admin token without any settings in the namespace and topics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/broker cherry-picked/branch-2.9 Archived: 2.9 is end of life doc-not-needed Your PR changes do not impact docs release/2.9.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants