BrokerAccessTokenProvider implements AccessTokenProvider { @Override public void refresh() { UserGroupInformation currentUser; UserGroupInformation loginUser; try { currentUser = UserGroupInformation.getCurrentUser(); loginUser = UserGroupInformation.getLoginUser(); } catch (IOException e) { throw new RuntimeException(e); } BrokerServerInfo serverInfo = Utils.getBrokerDetailsFromConfig(config); BrokerBaseCredentials credentials; if (tokenIdentifier == null) { credentials = new BrokerKerberosCredentials( serverInfo, currentUser.getUserName(), Collections.singleton(BrokerTokenIdentifier.GCS_SCOPE), Utils.getTarget(config, service)); } else { credentials = new BrokerSessionCredentials(serverInfo, tokenIdentifier.getSessionToken()); } loginUser.doAs((PrivilegedAction<Void>) () -> { try { credentials.refresh(); } catch (IOException e) { throw new RuntimeException(e); } return null; }); com.google.auth.oauth2.AccessToken token = credentials.getAccessToken(); accessToken = new AccessToken(token.getTokenValue(), token.getExpirationTime().getTime()); } BrokerAccessTokenProvider(Text service);  BrokerAccessTokenProvider(Text service, BrokerTokenIdentifier bti); @Override AccessToken getAccessToken(); @Override void refresh(); @Override void setConf(Configuration config); @Override Configuration getConf();  }
@Test public void testProviderRefreshWhileNotLoggedIn() { try { Configuration conf = TestingTools.getBrokerConfig(); refresh(conf); fail(); } catch (Exception e) { assertEquals(RuntimeException.class, e.getClass()); assertEquals( "Failed creating a SPNEGO token. Make sure that you have run kinit and that your Kerberos configuration is correct. See the full Kerberos error message: " + TGT_ERROR, e.getMessage() ); } }
@Test public void testProviderRefresh() throws IOException { TestingTools.startServer(new FakeServer(fakeKDC), grpcCleanup); Configuration conf = TestingTools.getBrokerConfig(); Subject alice = fakeKDC.login(ALICE); UserGroupInformation.loginUserFromSubject(alice); AccessToken token = refresh(conf); assertEquals("FakeAccessToken/AuthenticatedUser=" + ALICE + ";Owner=" + ALICE + ";Target=" + MOCK_BUCKET, token.getToken()); UserGroupInformation.setLoginUser(null); }
@Test public void testProviderRefreshWithoutAccessBoundary() throws IOException { TestingTools.startServer(new FakeServer(fakeKDC), grpcCleanup); Configuration conf = TestingTools.getBrokerConfig(); conf.set("gcp.token.broker.access.boundary.enabled", "false"); Subject alice = fakeKDC.login(ALICE); UserGroupInformation.loginUserFromSubject(alice); AccessToken token = refresh(conf); assertEquals("FakeAccessToken/AuthenticatedUser=" + ALICE + ";Owner=" + ALICE + ";Target=", token.getToken()); UserGroupInformation.setLoginUser(null); }