CubicLouve

Spring_MTの技術ブログ

ActiveRecordでEXPLAINをする

ActiveRecord::Relation には explain メソッドがあります。

github.com

このメソッドを使えば、ActiveRecord経由からでも EXPLAIN を打つことができます。

[11] pry > User.joins(:hoge).where(bar_id: 1).class
=> User::ActiveRecord_Relation
[12] pry > User.joins(:hoge).where(bar_id: 1).explain
=> EXPLAIN for: SELECT `users`.* FROM `users` INNER JOIN `hoges` ON `hoges`.`id` = `users`.`hoge_id` WHERE `users`.`bar_id` = 1
+----+-------------+----------+------------+--------+-----------------------------------------------------------------------------------------+---------------------------+---------+-----------------------------------------+------+----------+-------------+
| id | select_type | table    | partitions | type   | possible_keys                                                                           | key                       | key_len | ref                                     | rows | filtered | Extra       |
+----+-------------+----------+------------+--------+-----------------------------------------------------------------------------------------+---------------------------+---------+-----------------------------------------+------+----------+-------------+
|  1 | SIMPLE      | users    | NULL       | ref    | xxxx,xxxx | xxxx  | 4       | const                                   |    1 |     20.0 | Using where |
|  1 | SIMPLE      | hoges | NULL       | eq_ref | PRIMARY                                                                                 | PRIMARY                   | 4       | xxxx.users.hoge_id |    1 |    100.0 | Using index |
+----+-------------+----------+------------+--------+-----------------------------------------------------------------------------------------+---------------------------+---------+-----------------------------------------+------+----------+-------------+
2 rows in set (0.00 sec)

EXPLAINの内容は適当に変更しているので気にしないでください

ただしあくまで、リレーションにだけ適用できるので、find とかには使えません。

参照

railsguides.jp

Railsのcredentialの実装を追う + 自由にpathを指定してeditできるtaskを作る

スタートはここから

rails/credentials_command.rb at 0f5c8c5bc61b7e382e64cad4846406021bc8cd35 · rails/rails · GitHub

keyの生成

Rails::Generators::EncryptionKeyFileGenerator を使う これは

rails/encryption_key_file_generator.rb at 0f5c8c5bc61b7e382e64cad4846406021bc8cd35 · rails/rails · GitHub

中で ActiveSupport::EncryptedFile.generate_key を読んでいて

github.com

SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(CIPHER)) となっている。

ActiveSupport::MessageEncryptor.key_lenOpenSSL::Cipher.new(cipher).key_len をラップしているだけ。

ファイルの暗号化

rails/application.rb at 0f5c8c5bc61b7e382e64cad4846406021bc8cd35 · rails/rails · GitHub

ActiveSupport::EncryptedConfiguration が使われる

最終的に ActiveSupport::EncryptedFile に行き着く。

rails/encrypted_file.rb at 0f5c8c5bc61b7e382e64cad4846406021bc8cd35 · rails/rails · GitHub

taskの例

require "active_support/encrypted_file"

namespace :credentials do
  desc 'keyの生成'
  task generate_key: :environment do
    puts ActiveSupport::EncryptedFile.generate_key
  end

  desc 'ファイルの編集'
  task :edit_credential_file, ['path'] => :environment do |task, args|
    path = args.path
    credentials = ActiveSupport::EncryptedConfiguration.new(
      config_path: path,
      env_key: "RAILS_MASTER_KEY",
      key_path: "",
      raise_if_missing_key: true
    )
    credentials.change { |tmp_path| system(*Shellwords.split(ENV["EDITOR"]), tmp_path.to_s) }
    begin
      credentials.validate!
    rescue ActiveSupport::EncryptedConfiguration::InvalidContentError => error
      puts "WARNING: #{error.message}"
      puts "Your application will not be able to load '#{path}' until the error has been fixed."
    end
  end
end

NextAuth.jsのJWEをrubyで読み込む

まあ、あんまり用途がないと思いますが、調べたので書いておく。

肝は下記の部分

github.com

keyはKDFを通して生成する。

require 'jwe'
require 'hkdf'

next_auth_secret = "secretの内容"
jwe = ""

hkdf = HKDF.new(next_auth_secret, salt: '', algorithm: 'sha256', info: 'NextAuth.js Generated Encryption Key')
key = hkdf.read(32)

decrypt = JWE.decrypt(jwe, key)

puts decrypt

github.com

github.com

JWTのhelperがあるので、サーバーに送りたいときはこれを使ってJWTを送る next-auth.js.org

AWSに対してTerraformでなにかする

公式情報のリンクを載せておく

GitHub Actions github.com

TerraformのplanをPRに乗せる方法 developer.hashicorp.com

AWS のOIDC連携

github.com

docs.github.com

stateを保存するS3はバージョニングをonにする developer.hashicorp.com

GitHub ActionsのEnvironemnts

blog.shibayan.jp

rubyのメモリ周りのメモ

記事もろもろ

techracho.bpsinc.jp

www.joyfulbikeshedding.com

techlife.cookpad.com

jemallocに関するチケット

bugs.ruby-lang.org

malloc_trim(0)に関するチケット

bugs.ruby-lang.org

その他

ld(1)の --as-needed がデフォルトになった影響でSEGVする場合の対応

thatsdone-j.blogspot.com

bugs.ruby-lang.org

bugs.ruby-lang.org

github.com

LDFLAGS="-Wl,--no-as-needed" RUBY_CONFIGURE_OPTS="--disable-install-doc --enable-shared --with-jemalloc"

こんな感じでオプション設定が必要

ruby 3系であればパッチがあたっている模様