alfresco勉強会#31 alfresco 5.0にウイルススキャン機能を実装してみる
TRANSCRIPT
第 31回 Alfresco勉強会Alfresco 5.0にウイルススキャン機能を実装してみる
2016/01/27@_tasky
自己紹介 私は…
◦ @_tasky 日頃は…
◦ 株式会社イージフで Alfrescoや Liferayをいじってます◦ 2児 (5歳♀ , 2歳♀ )の父親業やってます◦ 最近はゆるふわエージェント業もやってます
Contents
ウイルススキャン機能の概要 ウイルススキャン機能の実装
◦ ポリシー◦ トランザクションリスナー◦ アクション
デモ
ウイルススキャン機能の概要 コンテンツアップロード時にウイルススキャンを実施する
◦ 新規ファイルアップロード時だけでなくファイル更新時にも対応する ウイルスが検出された場合はアップロードを中断し、当該ファイルをサーバ上から削除する Alfrescoはウイルススキャンの仕組みを持っていないので、外部のウイルススキャンツールを利用する
◦ コマンドライン実行可能なツールを想定する◦ お手軽なものだと Linuxで clam AV、WindowsでMicrosoft
Security Essentialsあたり?
ウイルススキャン機能の実装 では早速実装してきましょう ウイルススキャン機能の実装には以下の 3つの仕組みを使います
◦ ポリシー◦ トランザクションリスナー◦ アクション
ポリシー( Policies) コンテンツに関するイベントのようなもの 何らかの処理( Behaviourと呼ばれる)をポリシーに登録し、コンテンツに対する特定の操作時に任意の処理を実行する 以下のようなのポリシーが定義されている
◦ ContentServicePolicies OnContentUpdatePolicy…
◦ NodeServicePolicies OnCreateNodePolicy, OnUpdateNodePolicy, OnMoveNodePolicy,
OnUpdatePropertiesPolicy, OnDeleteNodePolicy◦ VersionServicePolicies
OnCreateVersionPolicy, OnRevertVersionPolicy…◦ …
ポリシー( Policies) PolicyComponentを使ってポリシーに Behaviourを登録する Behaviourで関連付けたメソッドを実装する
public class VirusScan extends TransactionListenerAdapter implements OnContentUpdatePolicy, InitializingBean {... public void afterPropertiesSet() throws Exception { this.policyComponent.bindClassBehaviour(ContentServicePolicies.OnContentUpdatePolicy.QNAME, this, new JavaBehaviour(this, "onContentUpdate")); }... public void onContentUpdate(NodeRef nodeRef, boolean newContent) { ... }...
トランザクションリスナー トランザクションに関するイベント処理のようなもの トランザクションリスナとその対象コンテンツをバインドし、トランザクションの以下のタイミングで任意の処理を実行する
◦ beforeCommit◦ beforeCompletion◦ afterCommit◦ afterRollback
トランザクションリスナー トランザクションリスナーとイベント処理を実装する
◦ TransactionListenerの実装もしくは TransationListenerAdapterの拡張 AlfrescoTransacionSupportを使ってトランザクションリスナーとコンテンツをバインドする
◦ AlfrescoTransactionListener#bindListener()◦ AlfrescoTransactionListener#bindResource()
public class VirusScan extends TransactionListenerAdapter implements OnContentUpdatePolicy, InitializingBean {... public void onContentUpdate(NodeRef nodeRef, boolean newContent) { ... AlfrescoTransactionSupport.bindListener(this); AlfrescoTransactionSupport.bindResource(NODE_KEY, nodeRefs); }... @Override public void beforeCommit(boolean readOnly) { List<NodeRef> nodeRefs = AlfrescoTransactionSupport.getResource(NODE_KEY); ...
アクション 単一のノードに対して行われる処理をユニット化したもの Spring bean定義で名前を付け、 ActionServiceを使って名前で呼び出すことが可能 Alfrescoの REST APIを利用して外部からアクションをキックすることもできる Spring bean定義で外部ツールの実行コマンドを記述することも可能
アクション アクションクラスを実装する
◦ ActionExecuterの実装もしくは ActionExecuterAbstractBaseの拡張 アクションクラスの Spring bean定義を記述する
◦ bean idがアクション名となる◦ RuntimeExecを利用すると、 OSごとの外部アプリケーションのコマンドライン呼び出しを記述可能public class VirusScanActionExecuter extends ActionExecuterAbstractBase {
... @Override protected void executeImpl(Action action, NodeRef nodeRef) { ...
<bean id="virus-scan-action" class="jp.aegif.alfresco.sample.action.executer.VirusScanActionExecuter" parent="action-executer">... <property name="virusScanCommand"> <bean class="org.alfresco.util.exec.RuntimeExec"> <property name="commandsAndArguments"> <map> <entry key="Windows.*"> <list> <value>C:/Program Files/Microsoft Security Client/MpCmdRun.exe</value> <value>-Scan</value> ... </list> </entry> <entry key="Linux"> ...
ウイルススキャン機能の実装 alfresco-virusscan-sample-context.xml
◦ Spring bean定義ファイル◦ <tomcat>/shared/classes/alfresco/extension/ に配置
VirusScan.java◦ OnContentUpdatePolicyを使ってファイルの更新を検知する◦ TransactionListener#beforeFommitを使ってファイル更新のコミット前にウイルススキャンアクションをキックする
VirusScanActionExecuter.java◦ 外部ツールをキックしてウイルススキャンを行うアクションクラス
VirusDetectException.java◦ ウイルス検知専用の Exception
デモ 環境
◦ Windows 7◦ Alfresco Community Edition 5.0.d◦ Microsoft Security Essentials
テスト用のファイル◦ EICARのWebサイトからテストファイルをダウンロードして利用する( http://www.eicar.org/85-0-Download.html)◦ リアルタイム検疫が有効になってるとウイルスファイルダウンロード直後に隔離されてしまうので無効にしておく
サンプルコード◦ Bitbucketで公開
https://bitbucket.org/tasuku_otani/alfrescovirusscansample
おまけ aegif Labo blog やってます
http://aegif-labo.blogspot.jp/