-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Http authentication support including None and Basic authentication
- Loading branch information
Showing
20 changed files
with
687 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/github/castorm/kafka/connect/http/auth/BasicHttpAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.github.castorm.kafka.connect.http.auth; | ||
|
||
/*- | ||
* #%L | ||
* Kafka Connect HTTP | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.auth.spi.HttpAuthenticator; | ||
import okhttp3.Credentials; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import static org.apache.commons.lang.StringUtils.isEmpty; | ||
|
||
public class BasicHttpAuthenticator implements HttpAuthenticator { | ||
|
||
private final Function<Map<String, ?>, BasicHttpAuthenticatorConfig> configFactory; | ||
|
||
Optional<String> header; | ||
|
||
public BasicHttpAuthenticator() { | ||
this(BasicHttpAuthenticatorConfig::new); | ||
} | ||
|
||
public BasicHttpAuthenticator(Function<Map<String, ?>, BasicHttpAuthenticatorConfig> configFactory) { | ||
this.configFactory = configFactory; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
|
||
BasicHttpAuthenticatorConfig config = configFactory.apply(configs); | ||
|
||
if (!isEmpty(config.getUser()) || !isEmpty(config.getPassword().value())) { | ||
header = Optional.of(Credentials.basic(config.getUser(), config.getPassword().value())); | ||
} else { | ||
header = Optional.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<String> getAuthorizationHeader() { | ||
return header; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/github/castorm/kafka/connect/http/auth/BasicHttpAuthenticatorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.github.castorm.kafka.connect.http.auth; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import lombok.Getter; | ||
import org.apache.kafka.common.config.AbstractConfig; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.common.config.types.Password; | ||
|
||
import java.util.Map; | ||
|
||
import static org.apache.kafka.common.config.ConfigDef.Importance.HIGH; | ||
import static org.apache.kafka.common.config.ConfigDef.Type.STRING; | ||
|
||
@Getter | ||
public class BasicHttpAuthenticatorConfig extends AbstractConfig { | ||
|
||
private static final String USER = "http.auth.user"; | ||
private static final String PASSWORD = "http.auth.password"; | ||
|
||
private final String user; | ||
private final Password password; | ||
|
||
BasicHttpAuthenticatorConfig(Map<String, ?> originals) { | ||
super(config(), originals); | ||
user = getString(USER); | ||
password = getPassword(PASSWORD); | ||
} | ||
|
||
public static ConfigDef config() { | ||
return new ConfigDef() | ||
.define(USER, STRING, "", HIGH, "Username") | ||
.define(PASSWORD, ConfigDef.Type.PASSWORD, "", HIGH, "Password"); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/github/castorm/kafka/connect/http/auth/ConfigurableHttpAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.github.castorm.kafka.connect.http.auth; | ||
|
||
/*- | ||
* #%L | ||
* Kafka Connect HTTP | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.auth.spi.HttpAuthenticator; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public class ConfigurableHttpAuthenticator implements HttpAuthenticator { | ||
|
||
private final Function<Map<String, ?>, ConfigurableHttpAuthenticatorConfig> configFactory; | ||
|
||
private HttpAuthenticator delegate; | ||
|
||
public ConfigurableHttpAuthenticator() { | ||
this(ConfigurableHttpAuthenticatorConfig::new); | ||
} | ||
|
||
public ConfigurableHttpAuthenticator(Function<Map<String, ?>, ConfigurableHttpAuthenticatorConfig> configFactory) { | ||
this.configFactory = configFactory; | ||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
|
||
ConfigurableHttpAuthenticatorConfig config = configFactory.apply(configs); | ||
|
||
delegate = config.getAuthenticator(); | ||
} | ||
|
||
@Override | ||
public Optional<String> getAuthorizationHeader() { | ||
return delegate.getAuthorizationHeader(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../java/com/github/castorm/kafka/connect/http/auth/ConfigurableHttpAuthenticatorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.github.castorm.kafka.connect.http.auth; | ||
|
||
/*- | ||
* #%L | ||
* kafka-connect-http | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.auth.spi.HttpAuthenticationType; | ||
import com.github.castorm.kafka.connect.http.auth.spi.HttpAuthenticator; | ||
import lombok.Getter; | ||
import org.apache.kafka.common.config.AbstractConfig; | ||
import org.apache.kafka.common.config.ConfigDef; | ||
|
||
import java.util.Map; | ||
|
||
import static org.apache.kafka.common.config.ConfigDef.Importance.MEDIUM; | ||
import static org.apache.kafka.common.config.ConfigDef.Type.STRING; | ||
|
||
@Getter | ||
public class ConfigurableHttpAuthenticatorConfig extends AbstractConfig { | ||
|
||
private static final String AUTH_TYPE = "http.auth.type"; | ||
|
||
private final HttpAuthenticator authenticator; | ||
|
||
ConfigurableHttpAuthenticatorConfig(Map<String, ?> originals) { | ||
super(config(), originals); | ||
authenticator = getAuthenticator(originals); | ||
} | ||
|
||
private HttpAuthenticator getAuthenticator(Map<String, ?> originals) { | ||
switch (HttpAuthenticationType.valueOf(getString(AUTH_TYPE).toUpperCase())) { | ||
case BASIC: | ||
BasicHttpAuthenticator auth = new BasicHttpAuthenticator(); | ||
auth.configure(originals); | ||
return auth; | ||
default: | ||
return new NoneHttpAuthenticator(); | ||
} | ||
} | ||
|
||
public static ConfigDef config() { | ||
return new ConfigDef() | ||
.define(AUTH_TYPE, STRING, HttpAuthenticationType.NONE.name(), MEDIUM, "Authentication Type"); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/castorm/kafka/connect/http/auth/NoneHttpAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.castorm.kafka.connect.http.auth; | ||
|
||
/*- | ||
* #%L | ||
* Kafka Connect HTTP | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
import com.github.castorm.kafka.connect.http.auth.spi.HttpAuthenticator; | ||
|
||
import java.util.Optional; | ||
|
||
public class NoneHttpAuthenticator implements HttpAuthenticator { | ||
|
||
@Override | ||
public Optional<String> getAuthorizationHeader() { | ||
return Optional.empty(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/github/castorm/kafka/connect/http/auth/spi/HttpAuthenticationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.castorm.kafka.connect.http.auth.spi; | ||
|
||
/*- | ||
* #%L | ||
* Kafka Connect HTTP | ||
* %% | ||
* Copyright (C) 2020 CastorM | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
|
||
public enum HttpAuthenticationType { | ||
NONE, BASIC | ||
} |
Oops, something went wrong.