2017年5月26日金曜日

laravel foreign key migration error

下記foreign keyを指定しようと、エラーとなった。

$table->foreign('dept_id')->references('id')->on('depts');

[Illuminate\Database\QueryException]                                  
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`c9`.`#sql-acb_114`, CONSTRAINT `employees_dept_id_foreign` FOREIGN KEY (`dept_id`) REFERENCES `depts` (
  `id`)) (SQL: alter table `employees` add constraint `employees_dept_id_foreign` foreign key (`dept_id`) references `depts` (`id`))

原因は、外部Keyを追加したいtableはすでに値が入っている場合、nullableと指定しないといけない
$table-> unsignedInteger ('dept_id')->after('gender')->nullable();

参考:https://laracasts.com/discuss/channels/laravel/foreign-key-error-on-migration

2017年5月25日木曜日

openAM IdP設定

1)SPのMetadataをDL
http://sp.xxxxx.jp/Shibboleth.sso/Metadata にアクセス、ファイルをDL
entityIDをSP側のApplicationDefaultsのentityIDと同じものに変更。複数ポートあるSPなら、それぞれのポートを指定し、ファイルをDL

2)MetadataをopenAMにセット
openAMの管理者画面から、Create SAMLv2 Providers->Register Remote Service Provider
「ファイル」を選択Metadataをアップロード
トラストサークルがない場合、新規作成。ある場合、既存のものに追加できる
「設定」ボタンを押すと、Remote Service Providerが追加される

3)属性マップの設定
FEDERATION(連携)画面の「エンティティープロバイダ」のIdP(http://openam.nijigo.local:8080/openam)をクリック、表明処理->属性マッパー
SPのattribute-map.xmlに定義しているnameと合うように設定、例えば
urn:oid:0.9.2342.19200300.100.1.1=uid
この設定によって、IdP側はuidの値(test1)をSP側にSAMLで渡す
<saml:Attribute Name="urn:oid:0.9.2342.19200300.100.1.1">
    <saml:AttributeValue
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="xs:string"
        >test1</saml:AttributeValue>

Shibboleth SPの設定

今回、SSO実現するためのSP側の設定となる。主に3つのXMLが中心的な役割を果たす。デフォルトの場所は、C:\opt\shibboleth-sp\etc\shibboleth にある
1)shibboleth2.xml、一番重要な設定ファイル。
①InProcess/ISAPI/Site
IISのSitesのIDとBindingポート番号と同じように設定する。また、nameはサイトのURLとなる。
 <Site id="1" name="sp.xxxxx.jp"/>
 <Site id="2" name="sp.xxxxx.jp" scheme="http" port="1116"/>
 <Site id="3" name="sp.xxxxx.jp" port="1117"/>

②RequestMapper/RequestMap/Host
nameは上記Siteのname:portとなる。アクセス認証必要な部分は、Pathで設定。今回の場合sp.xxxxx.jp/ssoにアクセスする場合、認証が必要。複数ポートがある場合、デフォルト以外のapplicationIdを指定する必要がある。
<Host name="sp.xxxxx.jp">
    <Path name="sso" authType="shibboleth" requireSession="true"/>
</Host>
<Host name="sp.xxxxx.jp:1116" applicationId="1116AAA">
    <Path name="sso" authType="shibboleth" requireSession="true"/>
</Host>
<Host name="sp.xxxxx.jp:1117" applicationId="1117BBB">
    <Path name="sso" authType="shibboleth" requireSession="true"/>
</Host>

③ApplicationDefaults
entityIDは任意の値でも良いが、一般的に以下のような形
http://sp.xxxxx.jp/shibboleth

SSO entityIDはIdP側のentityIDで、3)のmetadataにあるものと一致させる
http://openam.xxxxx.local:8080/openam
discovery serviceを使っていないため、discoveryProtocolとdiscoveryURL項目を削除

下記3)のmetadataを指定
<MetadataProvider type="XML" file="idp-metadata.xml"/>

複数ポートの場合、先定義したapplicationIdを使用する
<ApplicationOverride id="1116AAA" entityID="http://sp.xxxxx.jp:1116/shibboleth"/>
<ApplicationOverride id="1117BBB" entityID="http://sp.xxxxx.jp:1117/shibboleth"/>

2)attribute-map.xml
IdP側が送られたユーザー情報のattributeをどのようにマッピングするかの定義。デフォルトLDAP-based attributesがコメントしているため、コメントアウトする。このファイルが更新された場合、Shibboleth Daemonを再起動。

<Attribute name="urn:oid:0.9.2342.19200300.100.1.1" id="uid"/>
上記記述の意味は、IdPからPOSTされたSAMLの情報に、Name="urn:oid:0.9.2342.19200300.100.1.1"の値「test1」を$_SERVER["HTTP_UID"]に保存される(PHPの場合)

<saml:Attribute Name="urn:oid:0.9.2342.19200300.100.1.1">
    <saml:AttributeValue
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="xs:string"
        >test1</saml:AttributeValue>
SAMLファイルにあるNameはIdP側で設定する必要がある。別途IdPの設定を参照

3)以下のIdPサーバーにアクセスし、Matadataを入手、C:\opt\shibboleth-sp\etc\shibbolethにidp-metadata.xmlとして保存。このファイルには、IdPのアクセスポイント、証明書などが定義されている。
http://openam.xxxxx.local:8080/openam/saml2/jsp/exportmetadata.jsp?entityid=


参考:http://blogs.forgerock.org/petermajor/2010/09/how-to-access-federation-metadata-from-browser/

laravel5.4 DB migration

1)table作成
php artisan make:migration create_posts_table
php artisan make:migration create_categories_table
php artisan make:migration create_comments_table

database/migrationsに新たに3つファイルが作成された、それぞれ以下のように編集

2017_05_23_143039_create_posts_table
public function up()
 {
  Schema::create('posts', function($table){
   $table->increments('id');
   $table->string('title');
   $table->string('cat_id'); // ポストテーブルとカテゴリーテーブルの紐付けに利用します
   $table->text('content');
   $table->unsignedInteger('comment_count'); // 投稿に何件のコメントがついたのかをカウントします
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('posts');
 }

2017_05_23_143100_create_categories_table
public function up()
 {
  Schema::create('categories', function($table){
   $table->increments('id');
   $table->string('name');
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('categories');
 }
2017_05_23_143113_create_comments_table
public function up()
 {
  Schema::create('comments', function($table){
   $table->increments('id');
   $table->unsignedInteger('post_id'); // ポストテーブルとコメントテーブルの紐付けに利用します
   $table->string('commenter');
   $table->text('comment');
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('comments');
 }
php artisan migrate でtableが作成される

下記エラーになる場合、対応が必要。(mysql5.7.7以降のバージョンなら対応不要)
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

app/Providers/AppServiceProvider.php に以下のように追加

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}


参考:https://manablog.org/laravel_bulletin_board/
https://laravel-news.com/laravel-5-4-key-too-long-error

IISとShibbolethのインストール

SSO実現するには、IdP側はopenAMを使用、SP側はShibbolethを使用。Shibbolethのインストール手順は以下の通り。

1)IISをインストール













2)IISインストールオプションに必要な項目を追加












3)ShibbolethのSPモジュールをインストール
https://shibboleth.net/downloads/service-provider/latest/にて使用するOSのパッケージをDLし、インストーラの指示通り実行。(再起動が必要)

4)IISの設定確認
①ISAPI Filters





②Handler Mappings







③ISAPI and CGI Restrictions

5)Shibboleth Daemon起動しているかどうかを確認




6)Shibbolethの状態を確認
http://localhost/Shibboleth.sso/Status にアクセスして、画面の最後のところに下記になっているなら、OK
<Status>
 <OK/>
</Status>


2017年5月23日火曜日

laravel5.4 say hello

laravelがインストール完了したあと、
1)コントローラーを作成
php artisan make:controller Hello

app/Http/Controllers/Hello.phpが作成され

2)routes/web.phpに下記を追加し、public/helloにアクセスしてみると「Hello World!」が表示される

Route::get('/hello',function(){
    return 'Hello World!';
});

3)routes/web.phpを変更してみる
Route::get('hello', 'Hello@index');

4)app/Http/Controllers/Hello.phpに下記を追加、再度public/helloにアクセスしてみる
public function index()
{
    return 'hello world from controller : )';
}

5)Viewを追加してみる
resources/views/hello.blade.phpを作成、以下を追加
<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>

        <link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                display: table;
                font-weight: 100;
                font-family: 'Lato';
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }

            .title {
                font-size: 96px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
            </div>
        </div>
    </body>
</html>

6)routes/web.phpに下記を追加
Route::get('/hello/{name}', 'Hello@show');

7)app/Http/Controllers/Hello.phpにshowファンクションを追加
public function show($name)
{
    return view('hello',array('name' => $name));
}

8)public/hello/{name}にアクセスしてみる

参考:https://tutorials.kode-blog.com/laravel-hello-world

cloud9にLaravel5をインストール

1)cloud9でPHP/Apache projectを作成

2)bashでcomposerの準備
sudo composer self-update
sudo composer global require "laravel/installer"
export PATH=~/.composer/vendor/bin:$PATH

3)PHP7にupdate
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install libapache2-mod-php7.0
sudo a2dismod php5
sudo a2enmod php7.0
sudo apt-get install php7.0-dom php7.0-mbstring php7.0-zip php7.0-mysql

4)laravelアプリを作成
laravel new {app-name}
https://lalatest-xxxxxc9.c9users.io/{app-name}/public/

5){app-name}/config/app.phpの設定
'timezone' => 'Asia/tokyo',
'locale' => 'ja',

6){app-name}/config/database.phpの設定
'mysql' => [
....
'database' => 'c9',
'username' => 'xxxxxc9',
'password' => '',
....

7)デバッグ機能を有効化(オプション)
{app-name}/config/app.phpに
'debug' => true,

参考:http://qiita.com/mike_fx/items/c3da0fcd3050d7ea2f26

2017年5月16日火曜日

AWS windows server 2012R2にopenamをインストール

1)AWSにwindows server 2012R2のインスタンスを追加
基本的に、GUIの指示に沿って、特に問題ない。セキュリティグループの設定もこの時点でしなかった

2)インスタンスに接続(リモートデスクトップ)
インスタンス一覧の「接続」ボタンを押せば、指示通りで問題ない

3)JDKインストール
今回は8にした。JAVA_HOME環境変数を設定

4)tomcatインストール
こちらは7のzipファイルをDLし(8だとopenamが動かない)、c:\tomcatに展開
CATALINA_HOME環境変数を「c:\tomcat」に設定

C:\tomcat\binにsetenv.batを作成
set CATALINA_OPTS=-Xmx2048m -XX:MaxPermSize=512m

5)openamをインストール
forgerockのHPでOpenAM Enterprise 13.0.0のwarファイルをDL
名前をopenam.warに変更しC:\tomcat\webappsにコピー
C:\tomcat\bin\startup.bat で起動

6)http://localhost:8080/openam/にアクセスすると、設定画面が出てくる

7)C:\Windows\System32\drivers\etc\hostsファイルに下記追加
awsのprivate IP openam.xxx.xxx

8)http://www.openam.jp/wp-content/uploads/techtips_vol1.pdf の手順で新しい設定を行う。ここで注意することは、Server URLには「.」2つ以上にする必要ある。あと、windowsサーバーの場合、firewallをoffにしてください。

参考:http://yomon.hatenablog.com/entry/2015/09/13/235857
https://t246osslab.wordpress.com/2014/10/08/openam%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E6%99%82%E3%81%AE%E6%B3%A8%E6%84%8F%E7%82%B9%E3%81%A8%E3%83%88%E3%83%A9%E3%83%96%E3%83%AB%E3%82%B7%E3%83%A5%E3%83%BC%E3%83%86%E3%82%A3/