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

INTERNAL: Add ScramSaslClient #869

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft

INTERNAL: Add ScramSaslClient #869

wants to merge 1 commit into from

Conversation

namsic
Copy link
Collaborator

@namsic namsic commented Jan 10, 2025

๐Ÿ”— Related Issue

  • jam2in/arcus-works#672

โŒจ๏ธ What I did

  • Scram ์ธ์ฆ์„ ์œ„ํ•œ ScramSaslClient๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
  • com.bolyartech.scram_sasl ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • Kafka์˜ ScramSaslClient ๊ตฌํ˜„์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ๊ธฐ์กด java client์˜ ์ฝ”๋“œ ๋ฐฐ์น˜๋‚˜ ์Šคํƒ€์ผ๊ณผ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€๋„ ํ™•์ธํ•ด ์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

  • ๋Œ€๋žต ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌํ˜„์œผ๋กœ SCRAM ์ธ์ฆ ์‚ฌ์šฉํ•˜์—ฌ ์บ์‹œ ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ScramSaslClientProvider.initialize(); ํ˜ธ์ถœํ•œ ๋‹ค์Œ๋ถ€ํ„ฐ SCRAM-SHA-256 mechanism ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š”๋ฐ,
    ์–ด๋Š ์œ„์น˜์— ๋‘๋Š” ๊ฒƒ์ด ์ข‹์€์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์–ด์„œ, ์šฐ์„ ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์™ธ๋ถ€์—์„œ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ๋กœ ํ…Œ์ŠคํŠธํ–ˆ์Šต๋‹ˆ๋‹ค.
  private static final String[] mechanism = {"SCRAM-SHA-256"};
  private static final String username = "user01";
  private static final String password = "passwd01";

  private ArcusClient newClient(boolean useCluster, boolean useBinaryProtocol) throws IOException {
    ConnectionFactoryBuilder cfb = new ConnectionFactoryBuilder()
        .setAuthDescriptor(new AuthDescriptor(mechanism, 
        new PlainCallbackHandler(username, password)));
    
    if (useBinaryProtocol) {
      cfb.setProtocol(Protocol.BINARY);
    }

    if (useCluster) {
      return ArcusClient.createArcusClient("127.0.0.1:2181", "servicecode-01", cfb);
    } else {
      return new ArcusClient(cfb.build(), AddrUtil.getAddresses("127.0.0.1:11211"));
    }

  }

  @Test
  public void testSasl() throws Exception {
    ScramSaslClientProvider.initialize();

    ArcusClient mc = newClient(true, true);
    Thread.sleep(10000);

    assertTrue(mc.set("namsic:kv01", 30, "value01").get());
    assertEquals("value01", mc.get("namsic:kv01"));
  }

์ด PR์ด ๋ฐ˜์˜๋˜๋Š” ์‹œ์ ๋ถ€ํ„ฐ binary protocol ์‚ฌ์šฉ ์‹œ SCRAM-SHA-256 ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
ascii protocol์—์„œ SASL ์ธ์ฆ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ด€๋ จ operation์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

public SASLStepOperation saslStep(SaslClient sc, byte[] challenge, OperationCallback cb) {
throw new UnsupportedOperationException();
}
public SASLAuthOperation saslAuth(SaslClient sc, OperationCallback cb) {
throw new UnsupportedOperationException();
}

@namsic namsic requested review from uhm0311 and oliviarla January 10, 2025 05:59
src/main/java/net/spy/memcached/auth/ScramSaslClient.java Outdated Show resolved Hide resolved

private final ScramMechanism mechanism;
private final CallbackHandler callbackHandler;
private final ScramClientFunctionalityImpl scfi;
Copy link
Collaborator

Choose a reason for hiding this comment

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

ํ•„๋“œ๊ฐ€ ์„ ์–ธ๋œ ํƒ€์ž…์ด ScramClientFunctionalityImpl์ธ ์ƒํƒœ์ธ๋ฐ, ์„ ์–ธ ํƒ€์ž…์œผ๋กœ ScramClientFunctionality๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ตฌํ˜„์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋‚˜์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ScramClientFunctionality ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ ์™ธ์—๋„ Java์˜ ์ผ๋ฐ˜์ ์ธ ๊ตฌํ˜„ ํŒจํ„ด๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์€ ์—†๋Š”์ง€ ์‹ ๊ฒฝ์จ์„œ ํ™•์ธํ•ด ์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

  • class ์œ„์น˜(package)๋Š” ์ ์ ˆํ•œ์ง€
  • ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— this.๋ฅผ ๋ถ™์ด๋Š” ๊ฒฝ์šฐ / ์ƒ๋žตํ•˜๋Š” ๊ฒฝ์šฐ
  • ๊ธฐํƒ€ ์ „๋ฐ˜์ ์ธ ์ฝ”๋“œ ๊ตฌ์กฐ ๋ฐ ์ฝ”๋“œ ํฌ๋งท

Copy link
Collaborator

Choose a reason for hiding this comment

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

์ฝ”๋“œ ์Šคํƒ€์ผ์€ @jhpark816 ๋‹˜์ด ๋ด์ฃผ์‹ค ๊ฒ๋‹ˆ๋‹ค.

Comment on lines 85 to 87
byte[] clientFinalMessage = this.scfi.prepareFinalMessage(
password, serverFirstMessage).getBytes();
Copy link
Collaborator

Choose a reason for hiding this comment

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

serverNonce์˜ ๊ฒ€์ฆ ๋ถ€๋ถ„๋„ ScramClientFunctionalityImpl ๊ฐ์ฒด๊ฐ€ ์•Œ์•„์„œ ์ง„ํ–‰ํ•ด์ฃผ๋‚˜์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

kafka์˜ ์•„๋ž˜ ๊ตฌํ˜„์— ๋Œ€์‘ํ•˜๋Š” ๋กœ์ง์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด์ง€์š”?

                case RECEIVE_SERVER_FIRST_MESSAGE:
                    this.serverFirstMessage = new ServerFirstMessage(challenge);
                    if (!serverFirstMessage.nonce().startsWith(clientNonce))
                        throw new SaslException("Invalid server nonce: does not start with client nonce");

์•„๋ž˜์™€ ๊ฐ™์ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
https://github.com/ogrebgr/scram-sasl/blob/5f3d1cbf71e534170c9f28fcee7fa8ec6bf77291/lib/src/main/java/com/bolyartech/scram_sasl/client/ScramClientFunctionalityImpl.java#L118

        Matcher m = SERVER_FIRST_MESSAGE.matcher(serverFirstMessage);
        if (!m.matches()) {
            mState = State.ENDED;
            return null;
        }

        String nonce = m.group(1);

        if (!nonce.startsWith(mClientNonce)) {
            mState = State.ENDED;
            return null;
        }

์˜ˆ์™ธ๋ฅผ ๋˜์ง€์ง€ ์•Š๊ณ  internal state ๋ณ€๊ฒฝ ํ›„ null ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋˜์–ด ์žˆ๋Š”๋ฐ,
ํ˜ธ์ถœํ•˜๋Š” ์ธก์—์„œ null check ํ›„ ์˜ˆ์™ธ ๋˜์ง€๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š”์ง€๋Š” ํ™•์ธ์ด ํ•„์š”ํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ํ˜ธ์ถœํ•˜๋Š” ์ธก์—์„œ null check ํ›„ ์˜ˆ์™ธ ๋˜์ง€๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š”์ง€๋Š” ํ™•์ธ์ด ํ•„์š”ํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.

ํ™•์ธํ•ด๋ณด๊ณ  ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋ฆฌ๋ทฐ ๊ณ„์† ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

null check ๋ฐ ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ์ง„ํ–‰ํ•˜๋ฉด, ์„œ๋ฒ„์— sasl ๋ช…๋ น์„ empty body์™€ ํ•จ๊ป˜ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ์„œ๋ฒ„ ์ž…์žฅ์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ตํ•˜์ง€ ์•Š์€ ๊ฒƒ์ด๋ฏ€๋กœ ์ธ์ฆ ์‹คํŒจ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์™ธ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด Operation์˜ initialize()์—์„œ RuntimeException ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š”๋ฐ,
์˜ˆ์™ธ ๋ฐœ์ƒ ์ดํ›„ ๊ตฌ์ฒด์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š”์ง€๊นŒ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์„œ๋ฒ„์— ์š”์ฒญ์„ ์ „์†กํ•˜์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

  @Override
  public void initialize() {
    try {
      byte[] response = buildResponse(sc);
      String mechanism = sc.getMechanismName();

      prepareBuffer(mechanism, 0, response);
    } catch (SaslException e) {
      // XXX:  Probably something saner can be done here.
      throw new RuntimeException("Can't make SASL go.", e);
    }
  }

๊ฒฐ๊ตญ์—๋Š” ๋‘˜ ๋ชจ๋‘ ์ธ์ฆ ๊ณผ์ •์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์€ ๋™์ผํ•˜๋ฉฐ, ํ›„์ž์˜ ๋™์ž‘์ด ๋” ๋งž๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์˜ˆ์™ธ ๋˜์ง€๋„๋ก ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@uhm0311 uhm0311 Jan 15, 2025

Choose a reason for hiding this comment

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

develop ๋ธŒ๋žœ์น˜ ์ตœ์‹  ๊ธฐ์ค€์œผ๋กœ Operation.initialize()๋Š” ํฌ๊ฒŒ 3๊ตฐ๋ฐ์—์„œ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค.

  • ์‘์šฉ์˜ Worker Thread
  • ์ž๋ฐ” ํด๋ผ์ด์–ธํŠธ์˜ IO Thread
  • ์ž๋ฐ” ํด๋ผ์ด์–ธํŠธ์˜ Auth Thread

1๋ฒˆ์€ ์‘์šฉ์˜ ์›Œํฌ๋กœ๋“œ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋ฏ€๋กœ ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.
2๋ฒˆ์€ IO Thread๊ฐ€ ์ •์ง€๋˜๊ณ , ์•„๋ฌด๋Ÿฐ ์—ฐ์‚ฐ ์š”์ฒญ๋„ ๋ณด๋‚ด์ง€ ๋ชปํ•ด์„œ ๋ชจ๋“  ์บ์‹œ ์—ฐ์‚ฐ์ด Timeout ์ฒ˜๋ฆฌ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
3๋ฒˆ์€ ์–ด๋–ค ์˜ํ–ฅ์ด ์žˆ์„์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

@oliviarla 2๋ฒˆ์˜ ์˜ํ–ฅ์— ๋Œ€ํ•œ ์ œ ์„ค๋ช…์ด ๋งž๋Š”์ง€ ํ™•์ธํ•ด์ฃผ์‹œ๊ณ , Exception์„ ์ „ํŒŒํ•˜๋Š” ๊ฒƒ๊ณผ ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ์ค‘ ๋ฌด์—‡์ด ์ข‹์„์ง€ ๊ฒ€ํ† ํ•ด์ฃผ์„ธ์š”.
@namsic 3๋ฒˆ์˜ ์˜ํ–ฅ์ด ์–ด๋–ค์ง€ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.

@namsic namsic force-pushed the namsic/SaslClient branch from 47e972f to 94a7e87 Compare January 15, 2025 07:27
@namsic namsic force-pushed the namsic/SaslClient branch from 94a7e87 to ae75385 Compare January 15, 2025 08:14
@namsic namsic marked this pull request as draft January 17, 2025 06:57
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.

2 participants