仮想サーバか物理サーバか調べたら変なの出てきた
ちょっと物理か仮想か調べることになったので調べてたら想定外のものが出てきたので
調査方法と他をめも
調べ方
やってみた
$ sudo dmidecode -t system # Product Name: KVMが出たら仮想 $ sudo dmidecode -s system-serial-number # Not Specified が出たら仮想 # 上のコマンドのSerial Numberだけ表示
変なの出た
$ sudo dmidecode -t system # Product Nameを抜粋 Product Name: VMware Virtual Platform $ sudo dmidecode -s system-serial-number Serial Number: VMware XX XX XX # 謎のシリアル番号が出る
KVMとかNot Specifiedじゃないけどとても仮想くさい名前が出て困る
CakePHPでALLHTTPSに
CakePHPで全ページをhttpsにするのに苦労したときのメモが出てきたのでまとめてたら
当時の自分何してたんだってことに気づきました( ゚∀゚)
一般的な?やり方
SecurityコンポーネントとblackHoleCallbackを使って
こんな感じにAppControllerに以下追加
public $components = array('Security'); public function beforeFilter(){ if( $_SERVER['SERVER_PORT'] == 80 ) { // 何らかの手段でhttpであることを判定させる $this->Security->blackHoleCallback = 'forceHttps'; // ↓でアクション単位の設定ができ, 指定なしなので全アクションhttpsになる $this->Security->requireSecure(); } } public function forceHttps() { // $this->here でURIを取れる $this->redirect('https://' . env('SERVER_NAME') . $this->here); }
でもapacheでhttp→httpsにしたところ、
既に特殊Routingを組まれているせいで上記がこけたのと
Controllerでやるのはなぁと思っていたので下記対応を入れてみた
routesに書く
Router::fullbaseUrl('https://' . $_SERVER["HTTP_HOST"]);
lib/Cake/Routing/Router.phpを見ると
public static function fullBaseUrl($base = null) { if ($base !== null) { // fullbaseUrlを設定すると self::$_fullBaseUrl = $base; Configure::write('App.fullBaseUrl', $base); // 上書きされる! } if (empty(self::$_fullBaseUrl)) { // 設定されなかったら self::$_fullBaseUrl = Configure::read('App.fullBaseUrl'); // 設定を見る } return self::$_fullBaseUrl; }
こんな感じでConfigのApp.fullBaseUrlが上書きされている
なので
普通にConfigでやればよかった!
Configure::write('App.fullBaseUrl', 'https://'.$_SERVER["HTTP_HOST"]);
当時の自分、相当必死だったんだろうなぁ...と思った
個人的には
$this->Security->blackHoleCallback = 'forceHttps';
の見た目が気持ち悪くて苦手
cakePHPでapacheでリダイレクトさせようとしたけどだめだったよ
諦めたというオチです
やったこと
RewriteRule ^/helen https://www.google.co.jp/ [R=302,L]
オチ
apacheでリダイレクトできないけど
header('Location: https://www.google.co.jp/');
はしたくなかったのでapp/Config/routes.phpに下記設定。
ref) ルーティング
// 内部遷移 Router::redirect( '/helen/index/', array('controller' => 'hoge', 'action' => 'fuga'), array('status' => 301) ); // もちろん/helen/だけでも外部サイトでも行ける Router::redirect( '/helen/*', 'https://www.google.co.jp/', array('status' => 301) );
おまけ
cakePHPに入っているラスボスはこんな.htaccessです
app/.htaccess at master · cakephp/app · GitHub
ログを見るとこんな感じ。多いのでちょっと整形しました。
DocumentRootは/home/vagrant/webRoot/です
[rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/helen -> helen [rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'helen' [rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-d' => matched [rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-f' => matched [rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] rewrite 'helen' -> 'index.php' [rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] add per-dir prefix: index.php -> /home/vagrant/webRoot/index.php [rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] strip document_root prefix: /home/vagrant/webRoot/index.php -> /index.php [rid#7f8af0cdc7f8/initial] (1) [perdir /home/vagrant/webRoot/] internal redirect with /index.php [INTERNAL REDIRECT] [rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/index.php -> index.php [rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'index.php' [rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-d' => matched [rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-f' => not-matched [rid#7f8af0cee110/initial/redir#1] (1) [perdir /home/vagrant/webRoot/] pass through /home/vagrant/webRoot/index.php
分解すると
# /helenのアクセスのDocumentRootをはずす [rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/helen -> helen # RewriteRuleの ^ をhelenに適応できるか? [rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'helen' # RewriteCondそれぞれ確認 [rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-d' => matched [rid#7f8af0cdc7f8/initial] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/helen' pattern='!-f' => matched # matchedなのでhelen -> index.phpへ書き換え [rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] rewrite 'helen' -> 'index.php' # DocumentRootをつける [rid#7f8af0cdc7f8/initial] (3) [perdir /home/vagrant/webRoot/] add per-dir prefix: index.php -> /home/vagrant/webRoot/index.php # はずす [rid#7f8af0cdc7f8/initial] (2) [perdir /home/vagrant/webRoot/] strip document_root prefix: /home/vagrant/webRoot/index.php -> /index.php # /index.phpへリダイレクト [rid#7f8af0cdc7f8/initial] (1) [perdir /home/vagrant/webRoot/] internal redirect with /index.php [INTERNAL REDIRECT]
ここでridが変わる
# またDocumentRootを外す [rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] strip per-dir prefix: /home/vagrant/webRoot/index.php -> index.php # 条件にマッチするか確認するよ [rid#7f8af0cee110/initial/redir#1] (3) [perdir /home/vagrant/webRoot/] applying pattern '^' to uri 'index.php' # 条件だよ [rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-d' => matched [rid#7f8af0cee110/initial/redir#1] (4) [perdir /home/vagrant/webRoot/] RewriteCond: input='/home/vagrant/webRoot/index.php' pattern='!-f' => not-matched # これを表示するよ [rid#7f8af0cee110/initial/redir#1] (1) [perdir /home/vagrant/webRoot/] pass through /home/vagrant/webRoot/index.php
apacheのRewriteCondをサブネットマスクで許可する
192.168.33.0/25みたいなのを許可したい時
RewriteCond %{REMOTE_ADDR} !^(192\.168\.33\.0/25$)
じゃだめで
RewriteCond %{REMOTE_ADDR} !^(192\.168\.33\.([1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-6])$)
こうする
100からどっかまでの指定
126までにしたかったので
- 1[0-1][0-9] → 100〜119
- 12[0-6] → 120〜126
で指定
ひやっとしたね!
detached状態でmasterに-fプッシュした人がいるらしいよ
masterが突然ちぎれた対応をしたときのメモです
壊れちゃった!とヘルプを求められ、確認すると、
gitホスティングサービス上では不思議な事態が起こっていました。
こんな感じでmasterがちぎれていました
何をした結果なのか詳細は確認してないのですが、
なんらかの作業をした結果detached状態になり、
-fをつけてmasterプッシュしたのかな?という気がしています
目標は、masterを可能な限り復元することです
リポジトリの運用ルールに100%準拠していることを前提に、
そのリポジトリ利用者に、最後にmasterへマージしたdevelopを教えていただき
そこをmasterとして修正しました
手順
- (ローカルが死んでしまったため)リポジトリを新規クローン
- 最後にmasterへマージしたdevelopをcheckout
- detached状態のmasterを削除(ウワーッ)
- そのまま最後にmasterへマージしたdevelopからmasterを生やします
- 最後にmasterへマージしたdevelopと作成したmasterの最新コミットが一致することを一応確認
- -fプッシュ!
作業ログ
~/repo ❯❯❯ git branch * master ~/repo ❯❯❯ git log -1 commit !!!!!!!!FakeLastMasterCommit!!!!!!!! Author: XXXXXXXXXXXXXXXXXXX detachedしちゃったぜ! ~/repo ❯❯❯ git checkout last-develop Checking out files: 100% (6341/6341), done. Branch last-develop set up to track remote branch last-develop from origin. Switched to a new branch 'last-develop' ~/repo ❯❯❯ git log -1 commit !!!!!!!!RealLastCommit!!!!!!!! Merge: piyyoyo honyanya Author: YYYYYYYYYYYYYYYYYYYYYY 最後にmasterマージしたdevelopの最新コミットだよ ~/repo ❯❯❯ git branch -d master warning: deleting branch 'master' that has been merged to 'refs/remotes/origin/master', but not yet merged to HEAD. Deleted branch master (was hogege). ~/2/repo ❯❯❯ git checkout -b master Switched to a new branch 'master' ~/repo ❯❯❯ git log -1 commit !!!!!!!!RealLastCommit!!!!!!!! Merge: piyyoyo honyanya Author: YYYYYYYYYYYYYYYYYYYYYY 最後にmasterマージしたdevelopの最新コミットだよ ~/repo ❯❯❯ git push origin -f Total 0 (delta 0), reused 0 (delta 0) To ZZZZZZZZZZZZZZZZZZZ + hogege...fugaga master -> master (forced update)
お前も-fプッシュかい!
という感じですが、そうでもしないと修正できなかったのです...
master削除とmasterへの-fプッシュは気持ち的にやりたくないです
リポジトリをforkして1回検証するくらいやりたくないです
自分がそのリポジトリの最新を落としたらローカルが死んでしまったため
旧masterの最新コミットを拾うことができず...
とはいえ、
今思えばそのリポジトリの作業者を当たれば本物のコミットを救出できた可能性はあったんだろうなー
.git/refs/heads/masterが本物のコミットだったよねー
あーやっちゃった
運用ルールに準拠していることを前提に修正したのですが
ルールがあるってありがたいね!準拠してるって信じてる!!