Add SSH Public key to DB.

This commit is contained in:
Kaitlyn Parkhurst 2021-06-13 15:27:43 -07:00
parent 01881db0d7
commit 7e3e64655d
5 changed files with 29 additions and 27 deletions

View file

@ -63,6 +63,7 @@ create TABLE node_attribute (
CREATE TABLE sshkeys (
id serial PRIMARY KEY,
name text not null,
public_key text not null,
created_at timestamptz not null default current_timestamp
);

View file

@ -115,21 +115,6 @@ __PACKAGE__->set_primary_key("id");
=head1 RELATIONS
=head2 machines
Type: has_many
Related object: L<MeshMage::DB::Result::Machine>
=cut
__PACKAGE__->has_many(
"machines",
"MeshMage::DB::Result::Machine",
{ "foreign.network_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 nodes
Type: has_many
@ -146,8 +131,8 @@ __PACKAGE__->has_many(
);
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-06-05 18:43:45
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:6IIpmu6yIntxXoIKJPNDQg
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-06-13 22:16:41
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3lj5OoiCwYG+OUBZkansAg
# You can replace this text with custom code or comments, and it will be preserved on regeneration

View file

@ -49,6 +49,11 @@ __PACKAGE__->table("sshkeys");
data_type: 'text'
is_nullable: 0
=head2 public_key
data_type: 'text'
is_nullable: 0
=head2 created_at
data_type: 'timestamp with time zone'
@ -67,6 +72,8 @@ __PACKAGE__->add_columns(
},
"name",
{ data_type => "text", is_nullable => 0 },
"public_key",
{ data_type => "text", is_nullable => 0 },
"created_at",
{
data_type => "timestamp with time zone",
@ -88,8 +95,8 @@ __PACKAGE__->add_columns(
__PACKAGE__->set_primary_key("id");
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-06-07 07:29:27
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZkAMiHjAwbPP1Z/h+5zAZQ
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-06-13 22:16:41
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:X6jROLdtteQy6qomHHipIQ
# You can replace this text with custom code or comments, and it will be preserved on regeneration

View file

@ -2,9 +2,6 @@ package MeshMage::Web;
use Mojo::Base 'Mojolicious', -signatures;
use MeshMage::DB;
use Minion;
use IPC::Run3;
use File::Path qw( make_path );
use File::Temp;
# This method will run once at server start
sub startup ($self) {

View file

@ -1,5 +1,8 @@
package MeshMage::Web::Plugin::MinionTasks;
use Mojo::Base 'Mojolicious::Plugin', -signatures;
use IPC::Run3;
use File::Path qw( make_path );
use File::Temp;
sub register ( $self, $app, $config ) {
@ -24,16 +27,25 @@ sub register ( $self, $app, $config ) {
});
$app->minion->add_task( generate_sshkey => sub ( $job, $comment ) {
my $key = $job->app->db->resultset('Sshkey')->create({
name => $comment,
});
run3( [ qw( ssh-keygen -t rsa -b 4096 -q -C ), $comment, '-N', '', '-f', $job->app->config->{sshkey}{store} . "/new_key" ] );
my $private_key = Mojo::File->new( $job->app->config->{sshkey}{store} . "/new_key" )->slurp;
my $public_key = Mojo::File->new( $job->app->config->{sshkey}{store} . "/new_key.pub" )->slurp;
unlink $job->app->config->{sshkey}{store} . "/new_key";
unlink $job->app->config->{sshkey}{store} . "/new_key.pub";
run3( [ qw( ssh-keygen -t rsa -b 4096 -q -C ), $comment, '-N', '', '-f', $job->app->config->{sshkey}{store} . "/" . $key->id ] );
my $key = $job->app->db->resultset('Sshkey')->create({
name => $comment,
public_key => $public_key
});
Mojo::File->new( $job->app->config->{sshkey}{store} . "/" . $key->id )->spurt( $private_key );
Mojo::File->new( $job->app->config->{sshkey}{store} . "/" . $key->id . ".pub" )->spurt( $public_key );
});
$app->minion->add_task( import_sshkey => sub ( $job, $comment, $private_key, $public_key ) {
my $key = $job->app->db->resultset('Sshkey')->create({
name => $comment,
name => $comment,
public_key => $public_key,
});
Mojo::File->new( $job->app->config->{sshkey}{store} . "/" . $key->id )->spurt( $private_key );