とーますメモ

Ruby on Rails / Goなどの学習メモ

【Lynis】Suggestionsの項目を無視する方法

以下の記事を参考
How to Perform Security Audits With Lynis on Ubuntu 16.04 | DigitalOcean

設定ファイルを編集

$ vi /etc/lynis/custom.prf

以下は設定例

# Lines starting with "#" are comments
# Skip a test (one per line)

# This will ignore separation of partitions test
skip-test=FILE-6310

# Is Nginx installed?
skip-test=HTTP-6622

# Is Apache installed?
skip-test=HTTP-6702

# Skip checking print-related services
skip-test=PRNT-2307
skip-test=PRNT-2308

# If a test id includes more than one test use this form to ignore a particular test
skip-test=SSH-7408:tcpkeepalive

【Lynis】[BOOT-5122] シングルユーザモードでもログインできなくする

自分用メモ。

以下方法を行うことでデータの損失その他あらゆる不具合、不都合が生じた場合についても、一切の責任を負いません。
あくまでも自己責任でお願いします。

Set a password on GRUB bootloader to prevent altering boot configuration (e.g. boot in single user mode without password) [BOOT-5122]
https://cisofy.com/lynis/controls/BOOT-5122/

パスワードがなくても
ログインできてしまう機能(シングルユーザモードでのログイン)にパスワードをかける。

1)パスワードのハッシュ生成

パスワードを入力し、ハッシュを生成
以下の「grub.pbkdf2.sha512.10000.xxxxxxxx.........」の箇所をコピー

$ grub-mkpasswd-pbkdf2
Enter password:
Reenter password:
PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.xxxxxxxx.........

2)設定ファイルの編集

/etc/grub.d/40_customを開く

$ sudo vi /etc/grub.d/40_custom

ファイルの末尾に以下の設定を追記。
superusersの"username"は任意のユーザ名を追加。
password_pbkdf2のusernameはsuperusersのユーザ名と同じものをつける。
また1)で作成したハッシュパスワードをユーザ名の後に半角スペースを入れた後にペーストする。
設定例

set superusers="username"  
password_pbkdf2 username grub.pbkdf2.sha512.10000.xxxxxxxx.........  

3)grubのアップデート

$ sudo update-grub

4)サーバを再起動

これでサーバを再起動すると上記で作成したIDとPW(※ハッシュ文字ではない)の入力が必要になる。
VPSを借りて、SSHでログインしていた場合は、先にWEBコンソールなどでGRUBログインを先にしていないと
SSHでログインできないので注意

[参考]
grub2 - How to password protect Grub menu - Ask Ubuntu
How to Password Protect Ubuntu’s Boot Loader

ドキュメントの追加(書込)と検索(読込)

前回の記事の続き。
thoames.hatenadiary.jp

前準備

その後、プロジェクト用のディレクトリを任意の場所に作成

例)デスクトップにsampleディレクトリを作成

$ cd Desktop
$ mkdir sample

Firebase CLIの設定

$ firebase init

続く画面では、FirestoreとHosting選んで設定を続ける。(ずっとEnterで良い)

Cloud Firestoreの設定

Realtime Databaseの後継バージョンらしいが、まだベータ版。ベータ版でも問題ないならこちらを選択したほうが良いらしい。
Webの管理画面から、[Database]を選択し、データベースを作成する。
作成するには「ロックモード」か「テストモード」かを選ぶ必要があるが、ここでは「テストモード」を選択する。

index.htmlの作成

デフォルトでpublic/index.htmlが生成されているので、
中身を以下に変更。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Sample Chat</title>
</head>
<body>
  <h1>Hello world!</h1>

  <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-firestore.js"></script>
  <script>
    // Initialize Firebase
    var config = {
      apiKey: "ウェブAPIキー",
      authDomain: "プロジェクトID.firebaseapp.com",
      databaseURL: "https://プロジェクトID.firebaseio.com",
      projectId: "プロジェクトID",
      storageBucket: "プロジェクトID.appspot.com",
      messagingSenderId: "XXXXXXXXXXXXXX"
    };
    firebase.initializeApp(config);
  </script>
</body>
</html>

ちなみにデフォルトのindex.htmlで使用されている「https://www.gstatic.com/firebasejs/5.7.0/firebase.js」を使用していると
以下の警告がコンソールに表示される。

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the CDN builds, these are available in the following manner
(replace with the name of a component - i.e. auth, database, etc):

https://www.gstatic.com/firebasejs/5.0.0/firebase-.js

ガイドにも説明があるが要は
プロダクション環境だと、全てが固まっているjsを読み込むのではなく、必要なものを
個別に読み込んだほうが望ましいとのこと。
詳しくはガイド参照。

Firestoreを使ってデータの追加と検索を行う。

追加

以下の記述を内に追加

<button id="add_name">Add</button>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(function() {
$('#add_name').on('click', function() {
  var usersCollectionRef = db.collection("users");

  usersCollectionRef.add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
  })
  .then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
  })
  .catch(function(error) {
    console.error("Error adding document: ", error);
  });
});
});
</script>

index.htmlをブラウザで表示し、「Add」ボタンをクリックすると、データがFirestoreに追加される。

ちなみに最初は以下の記述抜きでコードを書いていたが

  db.settings({
    timestampsInSnapshots: true
  });

以下のエラーが出たため、上記のコードを入れた。

The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the
following code to your app before calling any other Cloud Firestore methods:

取得

取得についての詳細は以下のqiitaを参考にすると良い。

[参考]
Firebase Cloud Firestoreの使い方 - Qiita

Mac上でFirebaseを初期設定を行う

既にHomebrewが入っていて、
Firebase用のGoogleアカウントを既に所持していることが前提。

後述するFirebase CLIのセットアップでは
・Database
・Firestore
・Functions
・Hosting
・Storage
が選択できるが、
自分の場合は、FirestoreとHostingを選択する。
※用途によって選択する。

[参考]
Firebaseの各機能を3行で説明する - Qiita

インストール

nodebrew

Node.jsをバージョン管理できるnodebrewを先にインストールする。

$ brew install nodebrew

以下のコマンドをうち、表示されたパスを~/.bash_profileにExportする。

$ nodebrew setup

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile

node.jsをインストール([stable:安定版]をインストール)

$ nodebrew install-binary stable

確認

$ nodebrew list
v10.14.2

current: none

安定版を使用する設定

$ nodebrew use stable
$ node -v
v10.14.2

npmの更新

$ npm update -g npm

firebase

インストール

$ npm install -g firebase-tools

ログイン

https://firebase.google.comを開き、コンソールにアクセスする。
コンソールにて、任意のプロジェクトを作成する。

その後、以下のコマンドでログインを行う。

$ firebase login

すると以下のような「匿名の使用情報やエラーレポート情報を収集しても良いか?」と聞かれるので
Y/nで回答する。

"? Allow Firebase to collect anonymous CLI usage and error reporting information? (Y/n)"

するとブラウザで開くので、対象のGoogleアカウントを選択肢、Firebase CLIに権限許可を与えると、ログインに成功する。


サンプルアプリは次の記事で紹介する。

[参考]
【2018年版】macのhomebrewでnodebrew入れてからnode.jsを入れるまで - Qiita

Mac上でFirebaseを初期設定を行う

既にHomebrewが入っていて、
Firebase用のGoogleアカウントを既に所持していることが前提

インストール

nodebrew

Node.jsをバージョン管理できるnodebrewを先にインストールする。

$ brew install nodebrew

以下のコマンドをうち、表示されたパスを~/.bash_profileにExportする。

$ nodebrew setup

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile

node.jsをインストール([stable:安定版]をインストール)

$ nodebrew install-binary stable

確認

$ nodebrew list
v10.14.2

current: none

安定版を使用する設定

$ nodebrew use stable
$ node -v
v10.14.2

npmの更新

$ npm update -g npm

firebase

インストール

$ npm install -g firebase-tools

ログイン

https://firebase.google.comを開き、コンソールにアクセスする。
コンソールにて、任意のプロジェクトを作成する。

その後、以下のコマンドでログインを行う。

$ firebase login

すると以下のような「匿名の使用情報やエラーレポート情報を収集しても良いか?」と聞かれるので
Y/nで回答する。

"? Allow Firebase to collect anonymous CLI usage and error reporting information? (Y/n)"

するとブラウザで開くので、対象のGoogleアカウントを選択肢、Firebase CLIに権限許可を与えると、ログインに成功する。


サンプルアプリは次の記事で紹介する。

[参考]
【2018年版】macのhomebrewでnodebrew入れてからnode.jsを入れるまで - Qiita

【Mac】HomebrewでMySQLをインストールする

自分用メモ

※homebrewが既に入っている前提
※またSequel Proを使用するためには現時点では、デフォルトのMySQL8.0はサポートされていないので
追記の5.7をインストールする必要がある。

インストール

$ brew install mysql

2018/11/30時点だと、MySQL8.0がインストールされる。
この時点だとでログインしようとすると・・・

$ mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

とエラーが出る。

対応策は以下のサイトさんのやり方を踏襲
blog.kuromusubi.com
※同じやり方をしても何故かコマンドが通らない場合は、「flush privileges;」を叩いてみる。

自動起動有効化

brew servicesというコマンドで自動有効化できるっぽい。
qiita.com

$ brew services start mysql

これで、LaunchAgentsにシンボリックリンクを置かなくていいっぽい。

追記

Sequel Proが現時点では、8.0に対応していないので
5.7をインストールすることにした。

インストール

8.0を先にインストールしている場合は

$ brew uninstall mysql
$ rm -rf /usr/local/var/mysql

5.7をインストール

$ brew install mysql@5.7

mysql@5.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have mysql@5.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile

デフォルトだとパスが通ってないのでパスを通す。

$ echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile
$ soruce ~/.bash_profile

確認

$ mysql -v

rootにパスワードの設定

$ mysqladmin -u root password 'yourpassword'

最後に自動起動設定

$ brew services start mysql@5.7

my.cnfの場所

$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf

上記の順番で読み込まれる。

あまり環境を汚したくないので、以下の場所でmy.cnfを設定

$ vim /usr/local/etc/my.cnf


[参考]
Install MySQL 5.7 on macOS using Homebrew · GitHub
MySQL5.7をHomebrewでmacOSにインストールする手順 | WEB ARCH LABO

【WordPress】Wordmoveを使用して、本番環境と開発環境を同期させてみる

Wordmoveについての詳細は省くが、簡単に説明すると
Wordmoveとは、WordPressの環境を簡単に、移行させることができるRuby製のツール。

github.com

使用例としては、以下のような用途で使用できる。
・記事やプラグインの設定などを一度ローカルで行って問題がない場合、本番環境へ更新を行いたい。
・本番環境と同じデータやプラグインを使用し、ローカルでテストを行いたい。

インストール

※rubyがインストールされていることが前提

$ gem install wordmove

確認

$ wordmove -v

設定ファイル作成

以下のコマンドを使用することで、「Movefile」というファイルが作成される

$ wordmove init

Movefileの設定

global:
  sql_adapter: default  # wpcliがローカルにあるなら、wpcliを設定。

local:
  vhost: "ローカルURL"
  wordpress_path: "/var/www/html/" # use an absolute path here

  database:
    name: "DB名"
    user: "ユーザ"
    password: "パスワード"
    host: "ホスト"

production:
  vhost: "サイトURL"
  wordpress_path: "WPの絶対パス" # use an absolute path here

  database:
    name: "DB名"
    user: "ユーザ"
    password: "パスワード"
    host: "ホスト"

  exclude:
    - ".git/"
    - ".gitignore"
    - ".sass-cache/"
    - "node_modules/"
    - "bin/"
    - "tmp/*"
    - "Gemfile*"
    - "Movefile"
    - "wp-config.php"
    - "wp-content/*.sql"

  ssh:
    host: "リモートホスト"
    user: "ユーザ"
    port: 22
    rsync_options: "--verbose"

  forbid:
    push:
      db: true
      plugins: true
      themes: true
      languages: true
      uploads: true
      mu-plugins: true
    pull:
      db: false
      plugins: false
      themes: false
      languages: false
      uploads: false
      mu-plugins: false

globalセクションの「sql_adapter」では「default」か「wpcli」を選択できる。
sql_adapterの中身は、ローカルと本番でURLの変換するだけの機能。
defaultだと、dump.sqlテキスト内を走査して、文字列変換をしている。
wpcliではwp-cliのコマンドを使って、文字列変換をしている。

また他のサイトでは、あまり紹介していないが
間違って本番にpushしてしまわないようにforbidセクションを使用すれば、細かくpush可否の制限ができる。

Dockerイメージも公開されており、Dockerで開発環境を作る際は、すごい便利。
https://hub.docker.com/r/welaika/wordmove/

[リモートからローカルへ]
例1)DBのみをプルする例

$ wordmove pull -e production -d

例2)すべてをプル

$ wordmove pull -e production -all

例3)検証モードでプル(実際にはプルしない)

$ wordmove pull -e production -all -s

[ローカルからリモートへ]
例)DBのみをプッシュする例

$ wordmove push -e production -d

[オプション一覧]
--all # ファイル全部
-w # `/wp-content/`ディレクトリ(除外したファイル以外)のみ
-u # `/wp-content/upload/`ディレクトリのみ
-t # `/wp-content/theme/`のみ
-p # `/wp-content/plugins/`のみ
-l # `/wp-content/language/`のみ
-d # データベースファイルのみ
-v # [TBD] verbose
-s # dry runモードで実行、実際にはファイル転送されない
-e # 環境(push先・pull元)を指定
-c # `wp-config.php`のみ
-h # wordmoveの使い方


[参考]
Wordmoveの基本操作 - Qiita