AWS ELBのSecurity Groupの設定方法

ELB/ALBの下にサーバを配置するときにどのセキュリティグループにするかいつもわからなくなるのでメモ。 構成はシンプルでELB(https) -> Web(80)

ELBのSecurity Group

  • Inbound: HTTPS Custom 0.0.0.0/0
  • Outbound: WebのSecurity Group

WebのSecurity Group

  • Inbound: HTTP Custom 172.31.0.0/16 (VPCのサブネット)
  • Outbound: all, custom 0.0.0.0/0

Firebase storageのセキュリティ

FirebaseのAPIを使ってアップロードした場合、

https://firebasestorage.googleapis.com/v0/b/<project>.appspot.com/o/users%2F91b11d904a5f0eb398a334ssb5%2Fimages%2F60.jpg?alt=media&token=5908a2d0-7630-4a35-8be5-e3cc8adbd723


のようなtoken付きのlinkをgetDownloadLinkで取得できる。このトークンがあると、誰でもダンロードできてしまう。 ダウンロード対象のダウンロードオブジェクトがセキュリティに気を使わなくてよいなら、tokenごとDBに保存。そうでない場合は、ref(path)だけ保存しておいて、クライアント側で毎回getDownloadLinkするのが良さそうだ。

sqlite3でカラムを削除する方法 [SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE “users”]

Railsのマイグレーションでcolumnを追加後にrollbackすると下記のようなエラーができることがある。sqliteにはカラムを削除する方法がないので、一旦テーブルを消して作り直すのだが、外部キー制約のため削除できず、エラーになる。

== 20210802023517 AddJtiToUser: reverting =====================================
-- remove_index(:users, {:column=>:jti})
   -> 0.0022s
-- remove_column(:users, :jti, :string)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "users"


Caused by:
ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE "users"だ

だが、しかしsqlite3.35でカラム削除ができるようになったので、最新バージョンを使えば良い。手元のubuntuのsqlite3は、3.31だったので、ソースからコンパイルする。

$ wget https://www.sqlite.org/2021/sqlite-autoconf-3360000.tar.gz
$ tar zxvf sqlite-autoconf-3360000.tar.gz 
$ cd sqlite-autoconf-3360000/
$ ./configure 
$ make

でこのバージョンを使って、

$ sqlite-autoconf-3360000/sqlite3 ~/my_project/db/development.sqlite3

# delete index and then drop column

sqlite> drop index index_users_on_jti;
sqlite> ALTER TABLE users DROP COLUMN jti;

その後、Railsの場合、migration version を消すとrollbackしたことになる。

$ rails db:migrate:status | tail -n 2
   up     20210802023517  Add jti to user

$ sqlite3 db/development.sqlite3
> delete from schema_migrations where version='[version_number]';

 

 

wコマンドのParserをrubyで書いた。

/dev/pts以下にファイルがない時があったのでwコマンドをパースすることにした。(おそらくLXDコンテナ内だからかな)

  def idle_by_w_command
    cmd = "w | grep #{self.username} | awk '{print $5}'"
    output = self._exec(cmd, "admin")
    output.split("\n").map{|line|
      if line =~ /(\d+)days/
        $1.to_i * 3600 * 24
      elsif line =~ /(\d+):(\d+)m$/
        $1.to_i * 3600 + $2.to_i * 60
      elsif line =~ /(\d+):(\d+)$/
        $1.to_i * 60 + $2.to_i 
      elsif line =~ /([0-9]*[.])?[0-9]+s$/
        $1.to_i
      end
    }.compact
  end