-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from paviliondev/add_aws_gem_management
Add aws gem management
- Loading branch information
Showing
20 changed files
with
566 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
inherit_gem: | ||
rubocop-discourse: default.yml | ||
|
||
Discourse/NoAddReferenceOrAliasesActiveRecordMigration: | ||
Enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ GEM | |
|
||
PLATFORMS | ||
arm64-darwin-22 | ||
arm64-darwin-23 | ||
x86_64-linux | ||
|
||
DEPENDENCIES | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/jobs/scheduled/subscription_server/expire_stale_keys.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ::Jobs | ||
class SubscriptionServerExpireStaleKeys < ::Jobs::Scheduled | ||
every 1.week | ||
|
||
def execute(args) | ||
SubscriptionServer::UserResource.all.each do |user_resource| | ||
if user_resource.ready? && user_resource.iam_key_updated_at > 3.weeks.ago | ||
user_resource.expire_iam_keys! | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# frozen_string_literal: true | ||
|
||
module SubscriptionServer | ||
class UserResource < ActiveRecord::Base | ||
|
||
belongs_to :user | ||
|
||
def iam_ready? | ||
iam_user_name.present? && iam_access_key_id.present? && iam_secret_access_key.present? | ||
end | ||
|
||
def create_iam_user(iam) | ||
key = aws.create_user( | ||
user_name: self.user.username, | ||
group_name: iam[:group] | ||
) | ||
if key | ||
result = self.update( | ||
iam_user_name: key[:user_name], | ||
iam_access_key_id: key[:access_key_id], | ||
iam_secret_access_key: key[:secret_access_key], | ||
iam_key_updated_at: Time.now | ||
) | ||
end | ||
end | ||
|
||
def rotate_iam_key | ||
return unless self.iam_user_name | ||
|
||
key = aws.rotate_key(user_name: self.iam_user_name) | ||
if key | ||
self.update( | ||
iam_access_key_id: key[:access_key_id], | ||
iam_secret_access_key: key[:secret_access_key], | ||
iam_key_updated_at: Time.now | ||
) | ||
end | ||
end | ||
|
||
def expire_iam_keys! | ||
return unless self.iam_user_name | ||
|
||
aws.expire_keys(user_name: self.iam_user_name) | ||
end | ||
|
||
def aws | ||
@aws ||= SubscriptionServer::AWS.new | ||
end | ||
|
||
def self.list(user_id, subscriptions) | ||
result = [] | ||
|
||
subscriptions.each do |subscription| | ||
resource_name = subscription.resource | ||
resource = SubscriptionServer::Subscription.subscription_map[resource_name] | ||
next unless resource.present? | ||
|
||
user_resource = self.find_or_create_by( | ||
user_id: user_id, | ||
resource_name: resource_name | ||
) | ||
|
||
if resource[:iam] | ||
if !user_resource.iam_user_name | ||
user_resource.create_iam_user(resource[:iam]) | ||
elsif !user_resource.iam_access_key_id | ||
user_resource.rotate_iam_key | ||
end | ||
if user_resource.iam_key_updated_at > 1.week.ago | ||
user_resource.rotate_iam_key | ||
end | ||
next unless user_resource.iam_ready? | ||
end | ||
|
||
result << user_resource.reload | ||
end | ||
|
||
result | ||
end | ||
end | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: subscription_server_user_resources | ||
# | ||
# id :bigint not null, primary key | ||
# user_id :bigint not null | ||
# resource_name :string not null | ||
# iam_user_name :string | ||
# iam_access_key_id :string | ||
# iam_secret_access_key :string | ||
# iam_key_updated_at :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# idx_subscription_server_user_resources (user_id,resource_name) UNIQUE | ||
# index_subscription_server_user_resources_on_user_id (user_id) | ||
# |
19 changes: 19 additions & 0 deletions
19
app/serializers/subscription_server/user_resource_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class SubscriptionServer::UserResourceSerializer < ApplicationSerializer | ||
attributes :resource, | ||
:access_key_id, | ||
:secret_access_key | ||
|
||
def resource | ||
object.resource_name | ||
end | ||
|
||
def access_key_id | ||
object.iam_access_key_id | ||
end | ||
|
||
def secret_access_key | ||
object.iam_secret_access_key | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
...ription_server/subscription_serializer.rb → ...on_server/user_subscription_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
db/migrate/20240917062859_create_subscription_server_user_resources.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
class CreateSubscriptionServerUserResources < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :subscription_server_user_resources do |t| | ||
t.references :user, null: false | ||
t.string :resource_name, null: false | ||
t.string :iam_user_name | ||
t.string :iam_access_key_id | ||
t.string :iam_secret_access_key | ||
t.string :iam_key_updated_at | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :subscription_server_user_resources, | ||
%i[user_id resource_name], | ||
unique: true, | ||
name: :idx_subscription_server_user_resources | ||
end | ||
end |
Oops, something went wrong.