Skip to content

Commit

Permalink
Allow WRT to choose between replica and main database when generating…
Browse files Browse the repository at this point in the history
… Tokens (#8416)

* allow to choose between replica and main database

* De-duplicate token generation

* Simplify WRT DB token JS handling

* Do some styling on the form select

* Automatically pre-select right server in PMA dropdown

---------

Co-authored-by: Gregor Billing <[email protected]>
  • Loading branch information
FinnIckler and gregorbg authored Oct 13, 2023
1 parent d9deb47 commit fc4b65d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
22 changes: 17 additions & 5 deletions WcaOnRails/app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,26 @@ def do_generate_public_export
end

def generate_db_token
@db_endpoints = {
main: EnvConfig.DATABASE_HOST,
replica: EnvConfig.READ_REPLICA_HOST,
}

role_credentials = Aws::InstanceProfileCredentials.new
token_generator = Aws::RDS::AuthTokenGenerator.new credentials: role_credentials

@token = token_generator.auth_token({
region: EnvConfig.DATABASE_AWS_REGION,
endpoint: "#{EnvConfig.DATABASE_HOST}:3306",
user_name: EnvConfig.DATABASE_WRT_USER,
})
@db_tokens = @db_endpoints.transform_values do |url|
token_generator.auth_token({
region: EnvConfig.DATABASE_AWS_REGION,
endpoint: "#{url}:3306",
user_name: EnvConfig.DATABASE_WRT_USER,
})
end

@db_server_indices = {
main: 1,
replica: 2,
}
end

def check_regional_records
Expand Down
57 changes: 54 additions & 3 deletions WcaOnRails/app/views/admin/generate_db_token.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,64 @@
</p>

<pre>
<code><%= @token %></code>
<code id="token-content"></code>
</pre>

<label class="btn btn-primary" onclick="navigator.clipboard.writeText('<%= @token %>');">
<div class="form-group">
<label class="control-label" for="dbs">Choose a database:</label>

<select class="form-control" id="dbs" name="wrt-token">
<% @db_endpoints.each do |key, url| %>
<% db_token = @db_tokens[key] %>
<% db_server_index = @db_server_indices[key] %>

<option value="<%= key %>" data-wrt-token="<%= db_token %>" data-wrt-server-index="<%= db_server_index %>"><%= url %></option>
<% end %>
</select>
</div>

<br />

<label id="clipboard" class="btn btn-primary">
Copy to clipboard
</label>

<%= link_to t('layouts.navigation.results_database'), '/results/database/', class: 'btn btn-primary', target: '_blank' %>
<%= link_to t('layouts.navigation.results_database'), '/results/database/', id: 'db-link', class: 'btn btn-primary', target: '_blank' %>

<script>
const databaseDropdown = document.getElementById("dbs");

databaseDropdown.addEventListener("change", function (event) {
updateDbTokenAndLink(event.target);
});

function updateDbTokenAndLink(selectEl) {
const selectedOption = selectEl.options[selectEl.selectedIndex];
const selectedToken = selectedOption.dataset.wrtToken;

const tokenBox = document.getElementById('token-content');
tokenBox.innerText = selectedToken;

const dbLinkEl = document.getElementById('db-link');
const dbLinkUrl = new URL(dbLinkEl.href);

const uiServerIndex = selectedOption.dataset.wrtServerIndex;
dbLinkUrl.searchParams.set('server', uiServerIndex);

dbLinkEl.href = dbLinkUrl.toString();
}

document.getElementById("clipboard").addEventListener("click", copySelectedToClipboard);

function copySelectedToClipboard() {
const tokenBox = document.getElementById('token-content');
navigator.clipboard.writeText(tokenBox.textContent);
}

document.addEventListener("DOMContentLoaded", function() {
// trigger refresh with default data upon page load
updateDbTokenAndLink(databaseDropdown);
});
</script>
<% end %>
</div>

0 comments on commit fc4b65d

Please sign in to comment.