Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.kafka.clients.admin;

import java.util.Arrays;

/**
* Representation of a SASL/SCRAM Mechanism.
*
Expand All @@ -27,13 +29,15 @@ public enum ScramMechanism {
SCRAM_SHA_256((byte) 1),
SCRAM_SHA_512((byte) 2);

private static final ScramMechanism[] VALUES = values();

/**
*
* @param type the type indicator
* @return the instance corresponding to the given type indicator, otherwise {@link #UNKNOWN}
*/
public static ScramMechanism fromType(byte type) {
for (ScramMechanism scramMechanism : ScramMechanism.values()) {
for (ScramMechanism scramMechanism : VALUES) {
if (scramMechanism.type == type) {
return scramMechanism;
}
Expand All @@ -49,8 +53,10 @@ public static ScramMechanism fromType(byte type) {
* Salted Challenge Response Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms, Section 4</a>
*/
public static ScramMechanism fromMechanismName(String mechanismName) {
ScramMechanism retvalFoundMechanism = ScramMechanism.valueOf(mechanismName.replace('-', '_'));
return retvalFoundMechanism != null ? retvalFoundMechanism : UNKNOWN;
return Arrays.stream(VALUES)
.filter(mechanism -> mechanism.mechanismName.equals(mechanismName))
.findFirst()
.orElse(UNKNOWN);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@rondagostino looks like the original implementation didn't achieve the goal. I added a test that failed without this change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, you are right: valueOf() Throws: IllegalArgumentException - if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum type.

Thanks for catching/testing for/fixing it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@rondagostino do we need to cherry-pick this change to the 2.7 branch? I am not sure under which context this method is used and whether it's important enough to backport.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = key() != null ? key().hashCode() : 0;
int result = key().hashCode();
Comment thread
chia7712 marked this conversation as resolved.
result = 31 * result + Arrays.hashCode(value());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.kafka.clients.admin;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class ScramMechanismTest {

@Test
public void testFromMechanismName() {
assertEquals(ScramMechanism.UNKNOWN, ScramMechanism.fromMechanismName("UNKNOWN"));
assertEquals(ScramMechanism.SCRAM_SHA_256, ScramMechanism.fromMechanismName("SCRAM-SHA-256"));
assertEquals(ScramMechanism.SCRAM_SHA_512, ScramMechanism.fromMechanismName("SCRAM-SHA-512"));
assertEquals(ScramMechanism.UNKNOWN, ScramMechanism.fromMechanismName("some string"));
assertEquals(ScramMechanism.UNKNOWN, ScramMechanism.fromMechanismName("scram-sha-256"));
}

}
24 changes: 12 additions & 12 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ versions += [
bcpkix: "1.66",
checkstyle: "8.20",
commonsCli: "1.4",
gradle: "6.7",
gradleVersionsPlugin: "0.29.0",
grgit: "4.0.2",
gradle: "6.7.1",
gradleVersionsPlugin: "0.36.0",
grgit: "4.1.0",
httpclient: "4.5.12",
easymock: "4.2",
jackson: "2.10.5",
Expand Down Expand Up @@ -95,25 +95,25 @@ versions += [
lz4: "1.7.1",
mavenArtifact: "3.6.3",
metrics: "2.2.0",
mockito: "3.5.7",
mockito: "3.6.0",
netty: "4.1.51.Final",
owaspDepCheckPlugin: "5.3.2.1",
powermock: "2.0.7",
owaspDepCheckPlugin: "6.0.3",
powermock: "2.0.9",
reflections: "0.9.12",
rocksDB: "5.18.4",
scalaCollectionCompat: "2.2.0",
scalafmt: "1.5.1",
scalaJava8Compat : "0.9.1",
scalatest: "3.0.8",
scoverage: "1.4.1",
scoveragePlugin: "4.0.2",
shadowPlugin: "6.0.0",
scoveragePlugin: "5.0.0",
shadowPlugin: "6.1.0",
slf4j: "1.7.30",
snappy: "1.1.8.1",
spotbugs: "4.0.6",
spotbugsPlugin: "4.4.4",
spotlessPlugin: "5.1.0",
testRetryPlugin: "1.1.6",
spotbugs: "4.1.4",
spotbugsPlugin: "4.6.0",
spotlessPlugin: "5.8.2",
testRetryPlugin: "1.1.9",
zinc: "1.3.5",
zookeeper: "3.5.8",
zstd: "1.4.5-12"
Expand Down
16 changes: 6 additions & 10 deletions gradle/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ For a detailed description of spotbugs bug categories, see https://spotbugs.read
-->
<FindBugsFilter>

<!-- false positive in Java 11, see https://github.com/spotbugs/spotbugs/issues/756 -->
<Match>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>

<!-- false positive in Java 11, see https://github.com/spotbugs/spotbugs/issues/756 -->
<Match>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE"/>
</Match>

<Match>
<!-- Disable warnings about mutable objects and the use of public fields.
EI_EXPOSE_REP: May expose internal representation by returning reference to mutable object
Expand Down Expand Up @@ -103,6 +93,12 @@ For a detailed description of spotbugs bug categories, see https://spotbugs.read
</Or>
</Match>

<!-- false positive in Java 11, related to https://github.com/spotbugs/spotbugs/issues/756 but more complex -->
<Match>
<Class name="org.apache.kafka.common.record.KafkaLZ4BlockOutputStream"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>

<Match>
<!-- Suppression for the equals() for extension methods. -->
<Class name="kafka.api.package$ElectLeadersRequestOps"/>
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.