0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

今更GitLab Pagesを使おうとしたがaccess_controlを有効にすると動かないのを何とかした回

0
Last updated at Posted at 2026-06-06

GitLab Community Edition (GitLab Omnibus) 19.0.1 にて、GitLab Pagesを有効にして、かつ認証のためにaccess_controlを有効にすると、なぜかGitLab Pagesにアクセスできなかった。

まず普通にやろうとする。

/etc/gitlab/gitlab.rb
##! Pages access control
gitlab_pages['access_control'] = true
(他にもいろいろあるけど略)
gitlab-ctl reconfigure

そしてこんなログが出る。

(前略: 超長い)
[2026-06-07T00:38:46+09:00] WARN: Connecting to GitLab to generate new app_id and app_secret for GitLab Pages.
[2026-06-07T00:39:22+09:00] WARN: Something went wrong while executing gitlab-rails runner command to get or create the app ID and secret.
(後略: 超長い)

そしてもちろんGitLab Pagesにアクセスできない。
もっと悪いことに、↓のようにログを増やすオプションを足しても何一つヒントが増えなかった。

gitlab-ctl reconfigure --log-level debug

検索してもAIに聞いても、ネットワークの設定が間違ってるとかそういう感じのことが返ってきたが、原因は全く違うところにあった。

問題のログを出す処理を探す

問題のログのテキストをGitLabのソース内で探すと、このファイルにたどり着いた。
https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/19-0-stable/files/gitlab-cookbooks/gitlab/libraries/helpers/authorizer_helper.rb?ref_type=heads

PC上では通常は/opt/gitlab/embedded/cookbooks/gitlab/libraries/helpers/authorize_helper.rbに配置されている。

確かに WARN: Connecting to GitLab to generate new app_id and app_secret for #{name}という文言がある。
とりあえず中身を見てみよう。

/opt/gitlab/embedded/cookbooks/gitlab/libraries/helpers/authorize_helper.rb
  def query_gitlab_rails(uri, name, oauth_uid, oauth_secret)
    warn("Connecting to GitLab to generate new app_id and app_secret for #{name}.")
    runner_cmd = create_or_find_authorization(uri, name, oauth_uid, oauth_secret)
    cmd = execute_rails_runner(runner_cmd)
    do_shell_out(cmd)
  end

  def create_or_find_authorization(uri, name, oauth_uid, oauth_secret)
    args = %(redirect_uri: "#{uri}", name: "#{name}")

    app = %(
      app = Doorkeeper::Application.where(#{args}).by_uid_and_secret("#{oauth_uid}", "#{oauth_secret}");
      app ||= Doorkeeper::Application.where({ redirect_uri: "#{uri}", name: "#{name}", uid: "#{oauth_uid}", secret: "#{oauth_secret}" }).create!
    )

    output = %(puts app.uid.concat(" ").concat(app.secret);)

    %W(
      #{app}
      #{output}
    ).join
  end

  def execute_rails_runner(cmd)
    %W(
      /opt/gitlab/bin/gitlab-rails
      runner
      -e production
      '#{cmd}'
    ).join(" ")
  end

  def warn(msg)
    Chef::Log.warn(msg)
  end

  def info(msg)
    Chef::Log.info(msg)
  end
end

ついでに、先のquery_gitlab_railsを呼んでいるファイルはこちらである。
WARN: Something went wrong while executing gitlab-rails runner command to get or create the app ID and secret.の文言はこちらに含まれている。
https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/19-0-stable/files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb?ref_type=heads

原因調査

コードを改変して、問題のコードが何をやろうとしてるのか取り出す。
それにしてもexecute_rails_runnerという名前だが、紛らわしいことに実際にはコマンド文字列を生成しているだけである。

/opt/gitlab/embedded/cookbooks/gitlab/libraries/helpers/authorize_helper.rb
  def query_gitlab_rails(uri, name, oauth_uid, oauth_secret)
    warn("Connecting to GitLab to generate new app_id and app_secret for #{name}.")
    runner_cmd = create_or_find_authorization(uri, name, oauth_uid, oauth_secret)
    cmd = execute_rails_runner(runner_cmd)
+    info(cmd)
    do_shell_out(cmd)
  end

そうするとログにこのような文字列が出てくる。

[2026-06-07T00:38:46+09:00] WARN: Connecting to GitLab to generate new app_id and app_secret for GitLab Pages.
[2026-06-07T00:38:46+09:00] INFO: /opt/gitlab/bin/gitlab-rails runner -e production '
      app = Doorkeeper::Application.where(redirect_uri: "https://pages.example.com/projects/auth", name: "GitLab Pages").by_uid_and_secret("******", "******");
      app ||= Doorkeeper::Application.where({ redirect_uri: "https://pages.example.com/projects/auth", name: "GitLab Pages", uid: "******", secret: "******" }).create!
    puts app.uid.concat(" ").concat(app.secret);'

このコードを手で実行してみよう。
シェルにこの文字列を突っ込むだけである。

/opt/gitlab/bin/gitlab-rails runner -e production '
      app = Doorkeeper::Application.where(redirect_uri: "https://pages.example.com/projects/auth", name: "GitLab Pages").by_uid_and_secret("******", "******");
      app ||= Doorkeeper::Application.where({ redirect_uri: "https://pages.example.com/projects/auth", name: "GitLab Pages", uid: "******", secret: "******" }).create!
    puts app.uid.concat(" ").concat(app.secret);'

例によって長いログが出てくる。
重要なのはこの部分である。

    ==> /var/log/gitlab/postgresql/current <==
2026-06-06_15:11:13.29434 ERROR:  new row for relation "oauth_applications" violates check constraint "check_77eda6baaa"
2026-06-06_15:11:13.29437 DETAIL:  Failing row contains (26, GitLab Pages, ******, ******, https://pages.example.com/projects/auth, , 2026-06-06 15:11:13.293336, 2026-06-06 15:11:13.293336, null, null, f, t, f, t, f, null, t).

check_77eda6baaa制約を満たさないので書き込めない」らしい。
これが何なのか調べた。

# gitlab-psql
psql (17.8)
Type "help" for help.

gitlabhq_production=# \d oauth_applications;
                                             Table "public.oauth_applications"
        Column        |            Type             | Collation | Nullable |                    Default
----------------------+-----------------------------+-----------+----------+------------------------------------------------
 id                   | integer                     |           | not null | nextval('oauth_applications_id_seq'::regclass)
 name                 | character varying           |           | not null |
 uid                  | character varying           |           | not null |
 secret               | character varying           |           | not null |
 redirect_uri         | text                        |           | not null |
 scopes               | text                        |           | not null | ''::text
 created_at           | timestamp without time zone |           |          |
 updated_at           | timestamp without time zone |           |          |
 owner_id             | integer                     |           |          |
 owner_type           | character varying           |           |          |
 trusted              | boolean                     |           | not null | false
 confidential         | boolean                     |           | not null | true
 expire_access_tokens | boolean                     |           | not null | false
 ropc_enabled         | boolean                     |           | not null | true
 dynamic              | boolean                     |           | not null | false
 organization_id      | bigint                      |           |          |
 device_code_enabled  | boolean                     |           | not null | true
Indexes:
    "oauth_applications_pkey" PRIMARY KEY, btree (id)
    "idx_oauth_applications_organization_id" btree (organization_id)
    "index_oauth_applications_on_owner_id_and_owner_type" btree (owner_id, owner_type)
    "index_oauth_applications_on_secret" btree (secret)
    "index_oauth_applications_on_uid" UNIQUE, btree (uid)
Check constraints:
    "check_75750847b8" CHECK (char_length(scopes) <= 2048)
    "check_77eda6baaa" CHECK (organization_id IS NOT NULL)
Foreign-key constraints:
    "fk_e2fdb31d70" FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE
Referenced by:
    TABLE "ai_settings" CONSTRAINT "fk_05f695565a" FOREIGN KEY (amazon_q_oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL
    TABLE "oauth_device_grants" CONSTRAINT "fk_308d5b76fe" FOREIGN KEY (application_id) REFERENCES oauth_applications(id) ON DELETE CASCADE
    TABLE "ai_settings" CONSTRAINT "fk_4571bb0ccc" FOREIGN KEY (duo_workflow_oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL
    TABLE "application_settings" CONSTRAINT "fk_5d9b886930" FOREIGN KEY (workspaces_oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL
    TABLE "oauth_consents" CONSTRAINT "fk_c5f142ff4b" FOREIGN KEY (client_id) REFERENCES oauth_applications(uid) ON DELETE CASCADE
    TABLE "application_settings" CONSTRAINT "fk_f9867b3540" FOREIGN KEY (web_ide_oauth_application_id) REFERENCES oauth_applications(id) ON DELETE SET NULL

"check_77eda6baaa" CHECK (organization_id IS NOT NULL) と書かれている。つまり、書き込めないのはorganization_idNULLなのが原因である。

…では、organization_idには何を入れるべきかが問題になる。organizationなど触った覚えは全くないが、どうやらDefaultというorganizationが用意されているようだ。

↓参考:

実際にorganizationsテーブルの中身を見てみる。

gitlabhq_production=# SELECT * FROM organizations;
 id |          created_at           |          updated_at           |  name   |  path   | visibility_level | state
----+-------------------------------+-------------------------------+---------+---------+------------------+-------
  1 | 2023-08-25 13:33:53.780277+00 | 2023-08-25 13:33:53.780277+00 | Default | default |               20 |     4
(1 row)

というわけで、organization_id1を入れるように修正すればよさそうである。

解決へ

問題の処理を行っているファイルを改変する。

/opt/gitlab/embedded/cookbooks/gitlab/libraries/helpers/authorize_helper.rb
  def create_or_find_authorization(uri, name, oauth_uid, oauth_secret)
    args = %(redirect_uri: "#{uri}", name: "#{name}")

    app = %(
      app = Doorkeeper::Application.where(#{args}).by_uid_and_secret("#{oauth_uid}", "#{oauth_secret}");
-     app ||= Doorkeeper::Application.where({ redirect_uri: "#{uri}", name: "#{name}", uid: "#{oauth_uid}", secret: "#{oauth_secret}" }).create!
+     app ||= Doorkeeper::Application.where({ redirect_uri: "#{uri}", name: "#{name}", uid: "#{oauth_uid}", secret: "#{oauth_secret}", organization_id: 1 }).create!
    )

    output = %(puts app.uid.concat(" ").concat(app.secret);)

    %W(
      #{app}
      #{output}
    ).join
  end

この状態でgitlab-ctl reconfigureを実行する。

gitlab-ctl reconfigure
(略:超長い)
[2026-06-07T00:52:49+09:00] WARN: Connecting to GitLab to generate new app_id and app_secret for GitLab Pages.
[2026-06-07T00:53:27+09:00] INFO: Updated the gitlab-secrets.json file.
[2026-06-07T00:53:27+09:00] INFO: ruby_block[authorize pages with gitlab] called
[2026-06-07T00:53:27+09:00] INFO: ruby_block[re-populate GitLab Pages configuration options] called
()

成功したようだ。
そして無事にGitLab Pagesが動くようになった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?