OAuth2 ベースの Cosmos 認証プロバイダ

cosmos-hive-auth-provider とは何ですか?

cosmos-hive-auth-provider は、Hive 用のカスタム認証プロバイダです。Hive はネイティブにKerberos, PAM, LDAP などの認証を実装する多くの方法を提供しますが、このようなカスタム・メカニズムの設定も可能です。

cosmos-hive-auth-provider を使用することで、ユーザーは信頼できる Identity Manager (たとえばFIWARE Lab の Identity Manager)によって処理される OAuth2 トークン・ジェネレータ(サードパーティ) によって生成された手段または OAuth2 トークンで認証できます。

このライブラリが実装された方法に関する利点は、すべてのユーザーとパスワード・ベースの Hive クライアントが引き続き機能することです。単純に、パスワード設定パラメータはトークン値を取ります。

トップ

インストール

前提条件

gitcosmos-hive-auth-provider ライブラリをダウンロードしてビルドするには、git ツールとMaven をそれぞれインストールする必要があります。上記のツールに関する詳細なインストール手順を提供することは、このドキュメントの目的ではありません。

もちろん、このライブラリは、Hive サービスが Hadoop クラスタにデプロイされていないと意味がありません。

トップ

構築

fiware-cosmos リポジトリを Github で複製することから始めます :

$ git clone https://github.com/telefonicaid/fiware-cosmos.git

これで fiware-cosmos フォルダが作成されます。次に、以下のコマンドを実行してライブラリを構築します :

$ cd fiware-cosmos/cosmos-hive-auth-provider
$ git checkout release/x.y.z
$ mvn clean compile assembly:single
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building cosmos-hive-auth-provider 0.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ cosmos-hive-auth-provider ---
[INFO] Deleting /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cosmos-hive-auth-provider ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ cosmos-hive-auth-provider ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/target/classes
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ cosmos-hive-auth-provider ---
...
...
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
[INFO] org/ already added, skipping
[INFO] org/json/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.144 s
[INFO] Finished at: 2016-01-27T17:44:07+01:00
[INFO] Final Memory: 103M/1993M
[INFO] ------------------------------------------------------------------------

トップ

Jar のコピー

OAuth2AuthenticationProviderImpl クラスを含む、cosmos-hive-auth-provider jar は、Hive クラスパス内の1つのフォルダにコピーする必要があります :

$ ls /usr/lib/hive/lib/ | grep cosmos
cosmos-hive-auth-provider-0.0.0-SNAPSHOT-jar-with-dependencies.jar

トップ

単体テスト

単体テストは、このパラメータ化された mvn test コマンドを呼び出すことによって実行されます。

$ mvn test -Duser=frb -Dtoken=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building cosmos-hive-auth-provider 0.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cosmos-hive-auth-provider ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ cosmos-hive-auth-provider ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cosmos-hive-auth-provider ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ cosmos-hive-auth-provider ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ cosmos-hive-auth-provider ---
[INFO] Surefire report directory: /home/fiware-portal/fiware-cosmos/cosmos-hive-auth-provider/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.telefonica.iot.cosmos.hive.authprovider.OAuth2AuthenticationProviderImplTest
Testing OAuth2AuthenticationProviderImpl.Authenticate, it must fail because the token does not exist
log4j:WARN No appenders could be found for logger (com.telefonica.iot.cosmos.hive.authprovider.HttpClientFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Testing OAuth2AuthenticationProviderImpl.Authenticate, it must success
Testing OAuth2AuthenticationProviderImpl.Authenticate, it must fail because the user and the token don't match
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.256 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.110 s
[INFO] Finished at: 2016-01-28T10:59:10+01:00
[INFO] Final Memory: 23M/481M
[INFO] ------------------------------------------------------------------------

トップ

構成

cosmos-hive-auth-provider 自体に関しては何も設定する必要はありませんが、Hive サービスは使用するために設定する必要があります。

カスタム認証プロバイダである、cosmos-hive-auth-provider およびそのOAuth2AuthenticationProviderImpl クラスを有効にするには、次のプロパティを `hive-site.xml に追加する必要があります :

<property>
   <name>hive.server2.authentication</name>
   <value>CUSTOM</value>
</property>

<property>
   <name>hive.server2.custom.authentication.class</name>
   <value>com.telefonica.iot.cosmos.hive.authprovider.OAuth2AuthenticationProviderImpl</value>
</property>

この他のプロパティは、Identity Manager エンドポイント (OAuth2 認証トークンを検証するエンドポイント) のデフォルト値を上書きする場合、 hive-site.xml に追加する必要があります :

<property>
   <name>com.telefonica.iot.idm.endpoint</name>
   <value>https://account.lab.fiware.org</value>
</property>

最後に、このプロパティは、偽装を有効にするために変更する必要があります。すべてのクエリは、実際のエンドユーザではなくユーザの Hive によって実行されます :

<property>
   <name>hive.server2.enable.doAs</name>
   <value>true</value>
</property>

トップ

実行

これは Hive によって直接使用されるライブラリです。HiveServer2 がそれを認識するために、クラスタマネージャ (Ambariなど) またはコマンドラインからサービスを再起動します :

$ (sudo) service hive-server2 restart

トップ

使用法

cosmos-hive-auth-provider ライブラリは、HiveServer2 クライアントが接続してユーザと OAuth2 トークンを渡すときに使用されます。たとえば、fiware-cosmos リポジトリ内で配布されている Hiveクライアント のいずれかを使用しているとします :

$ pwd
/home/centos/fiware-cygnus/resources/hiveclients/java/hiveserver2-client
$ mvn exec:java -Dexec.args="computing.cosmos.lab.fiware.org 10000 default frb xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hiveserver2-client 0.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ hiveserver2-client >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ hiveserver2-client <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ hiveserver2-client ---
Connecting to jdbc:hive2://computing.cosmos.lab.fiware.org:10000/default?user=frb&password=XXXXXXXXXX
remotehive> show tables;
frb_test
remotehive> describe frb_test;
name,string
job,string
age,int
remotehive> select * from frb_test;
han,smuggler,35
luke,jedi,32
leia,princess,28
r2d2,robot,15
remotehive>

ユーザ資格情報を渡すときは、Unix または LDAP パスワードの代わりに OAuth2 トークンが渡されます。

トップ

管理

ログ

HiveServer2 のトレースは、通常、/var/log/hive/hiveserver2.log にログされます。例えば、すべてがうまくいくなら、cosmos-hive-auth-provider に関するトレースを見つけることができます :

2016-01-27 15:59:17,587 INFO  [pool-5-thread-4]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(67)) - Doing request: GET https://account.lab.fiware.org/user?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx HTTP/1.1
2016-01-27 15:59:17,594 INFO  [pool-5-thread-4]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(78)) - Response received: {"organizations": [], "displayName": "frb", "roles": [{"name": "provider", "id": "106"}], "app_id": "8556cc76154f41b3b43d7b31f0699982", "email": "frb@tid.es", "id": "frb"}
2016-01-27 15:59:17,667 INFO  [pool-5-thread-4]: thrift.ThriftCLIService (ThriftCLIService.java:OpenSession(188)) - Client protocol version: HIVE_CLI_SERVICE_PROTOCOL_V6
2016-01-27 15:59:17,676 INFO  [pool-5-thread-4]: hive.metastore (HiveMetaStoreClient.java:open(297)) - Trying to connect to metastore with URI thrift://dev-fiwr-bignode-11.hi.inet:9083
2016-01-27 15:59:17,677 INFO  [pool-5-thread-4]: hive.metastore (HiveMetaStoreClient.java:open(385)) - Connected to metastore.

トークンが存在しない場合、これは関連するトレースの例です :

2016-01-27 16:10:10,196 INFO  [pool-5-thread-28]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(67)) - Doing request: GET https://account.lab.fiware.org/user?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx HTTP/1.1
2016-01-27 16:10:10,197 INFO  [pool-5-thread-28]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(78)) - Response received: {"error": {"message": "Access Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx not found", "code": 404, "title": "Not Found"}}
2016-01-27 16:10:10,197 ERROR [pool-5-thread-28]: transport.TSaslTransport (TSaslTransport.java:open(296)) - SASL negotiation failure

トークンは存在しますが、指定されたユーザと一致しない場合、次のようなものが記録されます :

2016-01-27 16:12:11,520 INFO  [pool-5-thread-32]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(67)) - Doing request: GET https://account.lab.fiware.org/user?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx HTTP/1.1
2016-01-27 16:12:11,521 INFO  [pool-5-thread-32]: authprovider.HttpClientFactory (OAuth2AuthenticationProviderImpl.java:Authenticate(78)) - Response received: {"organizations": [], "displayName": "frb", "roles": [{"name": "provider", "id": "106"}], "app_id": "8556cc76154f41b3b43d7b31f0699982", "email": "frb@tid.es", "id": "frb"}
2016-01-27 16:12:11,521 ERROR [pool-5-thread-32]: transport.TSaslTransport (TSaslTransport.java:open(296)) - SASL negotiation failure
javax.security.sasl.SaslException: Error validating the login [Caused by javax.security.sasl.AuthenticationException: The given token does not match the given user]

トップ

キャッシュ

この認証プロバイダが OAuth2 トークンをキャッシュすることに注意することが重要です。キャッシングトークンは、クエリを Identity Manager に保存し、リフレッシュ・メカニズムを実装していないクライアント(たとえば、Cygnus)からの有効期限の切れたトークンを使用できるため便利です。

これはユーザには透過的ですが、管理上の観点からは、特定のログが表示される可能性があり、キャッシュのバックアップ・ファイルに関係します。

ログはキャッシュの使用に関するもので、可能性は次のとおりです :

17/03/02 09:14:20 INFO authprovider.OAuth2AuthenticationProviderImpl: User was not cached or token did not match, thus querying the IdM
...
17/03/02 09:14:20 INFO authprovider.OAuth2AuthenticationProviderImpl: User cached
...
17/03/02 09:14:29 INFO authprovider.OAuth2AuthenticationProviderImpl: User and token were cached, thus nothing to query to IdM
...

キャッシュ用のバックアップ・ファイルに関して、これは HiveServer2 の実行間でキャッシュを保存するファイルで、サーバが起動するたびに読み込まれ、キャッシュに変更が行われるたびに保存されます。常に /home/hive/oauth2.cache ファイルとして保存されます。このようなパスは現在設定可能ではありません。

トップ

問題のレポートと連絡先情報

問題を報告し、一般的な疑問を尋ねるのに適したチャネルがいくつかあります。それぞれは質問の性質に依存します :

  • このソフトウェアに関する特定の質問については、stackoverflow.com を使用してください。通常、これらはインストールの問題、エラー、バグに関係します。コードを作成する際の開発に関する質問も同様に歓迎されます。fiware-cosmos タグを使用します
  • FIWAREを使用している都市の数、アクセラレータ・プログラムへの参加方法などについては、ask.fiware.org を使用してください。このソフトウェアに関する一般的な質問(たとえば、議論したいユースケースやアーキテクチャなど)
  • 個人的なメール :

: コントリビュータがリクエストしない限り、コントリビュータに個人的に電子メールを送信しないようにしてください。実際にあなたがプライベートな E メールを送る場合、おそらく、stackoverflow.com または ask.fiware.org を使用するように強制する自動応答が送られてくるでしょう。これは、前述の方法を使用すると、将来のユーザにとって有用な知識の公開データベースが作成されるためです。プライベートメールはプライベートなもので、共有することはできません。

トップ