Commit 1357e835 authored by Jakob Tigerström's avatar Jakob Tigerström 🦊
Browse files

Removed un-neccesary files and added build script

parent f4a60786
# Base image: https://hub.docker.com/_/golang/
FROM golang:1.12.5-stretch
FROM golang:1.12.7-stretch
MAINTAINER Jakob Tigerström <jakob.tigerstrom@gmail.com>
# Install golint
......
#!/bin/bash
docker image prune -a
docker build . -t jakobtigerstrom/typhond:latest
docker push jakobtigerstrom/typhond:latest
CREATE USER 'typhond'@localhost;
GRANT ALL PRIVILEGES ON typhond.* TO 'typhond'@localhost;
UPDATE mysql.user SET Super_Priv='Y' WHERE user='typhond' AND host='localhost';
set global event_scheduler = ON;
FLUSH PRIVILEGES;
USE 'typhond';
DROP TABLE IF EXISTS secret;
DROP TABLE IF EXISTS userToRpc;
DROP TABLE IF EXISTS rpc;
DROP TABLE IF EXISTS user;
CREATE TABLE user(
username VARCHAR(60) UNIQUE,
blocked BOOL DEFAULT FALSE,
passwd VARCHAR(60),
dateUserCreated DATETIME,
userId INT PRIMARY KEY AUTO_INCREMENT,
toBeRemoved BOOL DEFAULT FALSE,
dateToEraseUser DATETIME
);
CREATE TABLE secret(
secretId INT PRIMARY KEY AUTO_INCREMENT,#unique identifier of the secret
userId INT,#connection to user
data MEDIUMBLOB,#The secret to store
secretName VARCHAR(60) NOT NULL ,#Name of the secret
dateSecretCreated DATETIME ,
dateToDie DATETIME DEFAULT 0,
useTimeToLive BOOL DEFAULT FALSE,
daysToUpdate INT DEFAULT 0,
CONSTRAINT FK_userSecret
FOREIGN KEY (userId) REFERENCES user(userId)
);
CREATE TABLE rpc(
rpcPrivId INT PRIMARY KEY AUTO_INCREMENT,
rpcName VARCHAR(20) UNIQUE
);
CREATE TABLE userToRpc(
userId INT,
rpcId INT,
CONSTRAINT FK_userToRpc
FOREIGN KEY (userId) REFERENCES user(userId),
CONSTRAINT FK_rpcToUser
FOREIGN KEY (rpcId) REFERENCES rpc(rpcPrivId),
UNIQUE KEY userRpc (userId,rpcId)
);
DROP PROCEDURE IF EXISTS lockUser;
DELIMITER //
CREATE PROCEDURE lockUser(IN userToBlockId int, IN userThatBlock int)
BEGIN
IF (SELECT validRpc(userThatBlock,"lockUser")) AND (SELECT blocked FROM user WHERE userThatBlock = userId) = FALSE THEN
UPDATE user SET blocked = TRUE WHERE userToBlockId = userId;
end if ;
SELECT username,blocked FROM user WHERE userToBlockId = userId;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS unlockUser;
DELIMITER //
CREATE PROCEDURE unlockUser(IN userToBlockId INT, IN userThatUnblock INT)
BEGIN
IF (SELECT validRpc(userThatUnblock,"lockUser")) AND (SELECT blocked FROM user WHERE userThatUnblock = userId) = FALSE THEN
UPDATE user SET blocked = FALSE WHERE userToBlockId = userId;
end if ;
SELECT username,blocked FROM user WHERE userToBlockId = userId;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS lockdown;
DELIMITER //
CREATE PROCEDURE lockdown(IN userThatLocks INT)
BEGIN
IF (SELECT validRpc(userThatLocks,"lockdown")) AND (SELECT blocked FROM user WHERE userThatLocks = userId) = FALSE THEN
UPDATE user SET blocked = TRUE;
UPDATE user SET blocked = FALSE WHERE userId = userThatLocks;
END IF ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS unLockdown;
DELIMITER //
CREATE PROCEDURE unLockdown(IN userThatUnlocks INT)
BEGIN
IF (SELECT validRpc(userThatUnlocks,"lockdown")) AND (SELECT blocked FROM user WHERE userThatUnlocks = userId) = FALSE THEN
UPDATE user SET blocked = FALSE;
END IF ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS removeSecret;
DELIMITER //
CREATE PROCEDURE removeSecret(IN idSecret INT, IN secretOwnerId INT)
BEGIN
IF (SELECT validRpc(secretOwnerId,"removeSecret")) AND (SELECT blocked FROM user WHERE secretOwnerId = userId) = FALSE THEN
DELETE FROM secret WHERE secretId = idSecret AND secretOwnerId = userId;
end if;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS removeUser;
DELIMITER //
CREATE PROCEDURE removeUser(IN idUser INT,IN userThatRemoves INT,IN days INT)
BEGIN
IF (SELECT validRpc(userThatRemoves,"removeUser")) AND (SELECT blocked FROM user WHERE userThatRemoves = userId) = FALSE THEN
UPDATE user SET toBeRemoved = TRUE , dateToEraseUser = (NOW() + INTERVAL days DAY) WHERE userId = idUser;
UPDATE secret SET dateToDie = (NOW() + INTERVAL days DAY), useTimeToLive = TRUE WHERE userId = idUser;
end if;
SELECT username,dateToEraseUser FROM user WHERE idUser = userId;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS dontRemoveUser;
DELIMITER //
CREATE PROCEDURE dontRemoveUser(IN idUser INT,IN userThatRevokeRemove INT)
BEGIN
IF (SELECT validRpc(userThatRevokeRemove,"dontRemoveUser")) AND (SELECT blocked FROM user WHERE userThatRevokeRemove = userId) = FALSE THEN
UPDATE user SET toBeRemoved = FALSE , dateToEraseUser = NULL;
UPDATE secret SET dateToDie = (NOW() + INTERVAL 30 DAY), useTimeToLive = FALSE WHERE userId = idUser;
end if;
SELECT username,toBeRemoved FROM user WHERE idUser = userId;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS listUserSecrets;
DELIMITER //
CREATE PROCEDURE listUserSecrets(IN idUser INT)
BEGIN
IF (SELECT validRpc(idUser,"listUserSecrets")) AND (SELECT blocked FROM user WHERE idUser = userId) = FALSE THEN
SELECT secretId,secretName,data,dateSecretCreated,dateToDie FROM secret INNER JOIN user u ON secret.userId = u.userId WHERE u.userId = idUser;
end if ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS listUsers;
DELIMITER //
CREATE PROCEDURE listUsers(IN userThatLists INT)
BEGIN
IF (SELECT validRpc(userThatLists,"listUsers")) AND (SELECT blocked FROM user WHERE userThatLists = userId) = FALSE THEN
SELECT userId,username,dateUserCreated,blocked FROM user;
end if ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS getSecret;
DELIMITER //
CREATE PROCEDURE getSecret(IN nameSecret varchar(255), IN secretOwnerId int)
BEGIN
IF (SELECT validRpc(secretOwnerId,"getSecret")) AND (SELECT blocked FROM user WHERE secretOwnerId = userId) = FALSE THEN
UPDATE secret SET dateToDie = (NOW() + INTERVAL daysToUpdate DAY) WHERE secretName = nameSecret AND secretOwnerId = userId;
SELECT secretName,data,dateSecretCreated,dateToDie FROM secret WHERE nameSecret = secretName AND secretOwnerId = userId;
END IF ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS addUser;
DELIMITER //
CREATE PROCEDURE addUser(IN newUserName varchar(60), IN pass varchar(60),IN userThatAdds INT)
BEGIN
DECLARE id INT DEFAULT 0;
IF (SELECT validRpc(userThatAdds,"addUser")) AND (SELECT blocked FROM user WHERE userThatAdds = userId) = FALSE THEN
INSERT INTO user (username,passwd,dateUserCreated) VALUE (newUserName,pass,NOW());
SET id = (SELECT userId FROM user WHERE username = newUserName);
CALL addRpcToUser("getSecret",userThatAdds,id);
CALL addRpcToUser("addSecret",userThatAdds,id);
CALL addRpcToUser("removeSecret",userThatAdds,id);
end if ;
SELECT username FROM user WHERE username = newUserName;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS addSecret;
DELIMITER //
CREATE PROCEDURE addSecret(IN nameSecret varchar(60), IN secretOwnerId int, IN secretData mediumblob, IN dayes int)
BEGIN
DECLARE x BOOL DEFAULT TRUE;
IF dayes <= 0 THEN
SET x = FALSE ;
end if ;
IF (SELECT validRpc(secretOwnerId,"addSecret")) = TRUE AND (SELECT blocked FROM user WHERE secretOwnerId = userId) = FALSE THEN
IF nameSecret NOT IN (SELECT secretName FROM secret WHERE secretId = secretOwnerId) THEN
INSERT INTO secret (userId, data, secretName,dateToDie,useTimeToLive,daysToUpdate,dateSecretCreated) VALUE (secretOwnerId,secretData,nameSecret,NOW() + INTERVAL dayes DAY,x,dayes,NOW());
end if ;
end if ;
SELECT secretId,secretName,dateSecretCreated FROM secret WHERE secretName = nameSecret AND secretOwnerId = userId;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS allSecrets;
DELIMITER //
CREATE PROCEDURE allSecrets(IN userThatLists INT)
BEGIN
IF (SELECT validRpc(userThatLists,"allSecrets")) = TRUE AND (SELECT blocked FROM user WHERE userThatLists = userId) = FALSE THEN
SELECT secretId,secretName,data,dateSecretCreated,dateToDie,(SELECT username FROM user WHERE secret.userId = user.userId) 'owner' FROM secret;
end if ;
end//
DELIMITER ;
DROP PROCEDURE IF EXISTS addRpc;
DELIMITER //
CREATE PROCEDURE addRpc(rpcToAdd VARCHAR(20),userThatAddsRpc INT)
BEGIN
IF (SELECT validRpc(userThatAddsRpc,"addRpc")) = TRUE AND (SELECT blocked FROM user WHERE userThatAddsRpc = userId) = FALSE THEN
INSERT INTO rpc (rpcName) VALUE (rpcToAdd);
end if ;
SELECT rpcName FROM rpc WHERE rpcName= rpcToAdd;
end //
DELIMITER ;
DROP PROCEDURE IF EXISTS addRpcToUser;
DELIMITER //
CREATE PROCEDURE addRpcToUser(rpcToAdd VARCHAR(20),userThatAddsRpc INT,userThatRecivesRpc INT)
BEGIN
DECLARE id INT DEFAULT 0;
IF (SELECT validRpc(userThatAddsRpc,"addRpcToUser")) = TRUE AND (SELECT blocked FROM user WHERE userThatAddsRpc = userId) = FALSE THEN
SET id = (SELECT rpcPrivId FROM rpc WHERE rpcName = rpcToAdd);
INSERT INTO userToRpc (userId, rpcId) VALUE (userThatRecivesRpc,id);
end if ;
SELECT id;
end //
DELIMITER ;
DROP PROCEDURE IF EXISTS listAllowedRpc;
DELIMITER //
CREATE PROCEDURE listAllowedRpc(userThatList INT)
BEGIN
IF (SELECT validRpc(userThatList,"listAllowedRpc")) = TRUE AND (SELECT blocked FROM user WHERE userThatList = userId) = FALSE THEN
SELECT rpcName FROM rpc WHERE rpcPrivId IN (SELECT rpcId FROM userToRpc WHERE userThatList = userid);
end if ;
end //
DELIMITER ;
DROP FUNCTION IF EXISTS validRpc;
CREATE FUNCTION validRpc(userAdmin INT, rpcNameCheck VARCHAR(20)) RETURNS BOOL
RETURN (SELECT userId FROM userToRpc WHERE rpcId IN (SELECT rpcPrivId FROM rpc WHERE rpcName = rpcNameCheck) AND userId = userAdmin) = userAdmin;
DROP PROCEDURE IF EXISTS superUser;
DELIMITER //
CREATE PROCEDURE superUser(pass VARCHAR(128))
BEGIN
IF EXISTS(SELECT userId FROM user) = FALSE THEN
INSERT INTO user (username,passwd,dateUserCreated) VALUE ("admin",pass,NOW());
INSERT INTO userToRpc (userId, rpcId) VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10),(1,11),(1,12),(1,13),(1,14);
end if ;
SELECT userId,username,dateUserCreated FROM user WHERE username = "admin";
end //
DELIMITER ;
DROP EVENT IF EXISTS checkTimeToLive;
DELIMITER //
CREATE EVENT `checkTimeToLive` ON SCHEDULE EVERY 1 DAY DO
BEGIN
DELETE FROM secret WHERE (useTimeToLive = TRUE ) AND (dateToDie < NOW());
DELETE FROM userToRpc WHERE userId IN (SELECT userId FROM user WHERE toBeRemoved = TRUE);
DELETE FROM user WHERE (toBeRemoved = TRUE) AND (dateToEraseUser < NOW());
end//
DELIMITER ;
#! /bin/sh
#
# Author: Bert Van Vreckem <bert.vanvreckem@gmail.com>
#
# A non-interactive replacement for mysql_secure_installation
#
# Tested on CentOS 6, CentOS 7, Ubuntu 12.04 LTS (Precise Pangolin), Ubuntu
# 14.04 LTS (Trusty Tahr).
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
#{{{ Functions
usage() {
cat << _EOF_
Usage: ${0} "ROOT PASSWORD"
with "ROOT PASSWORD" the desired password for the database root user.
Use quotes if your password contains spaces or other special characters.
_EOF_
}
# Predicate that returns exit status 0 if the database root password
# is set, a nonzero exit status otherwise.
is_mysql_root_password_set() {
! mysqladmin --user=root status > /dev/null 2>&1
}
# Predicate that returns exit status 0 if the mysql(1) command is available,
# nonzero exit status otherwise.
is_mysql_command_available() {
which mysql > /dev/null 2>&1
}
#}}}
#{{{ Command line parsing
if [ "$#" -ne "1" ]; then
echo "Expected 1 argument, got $#" >&2
usage
exit 2
fi
#}}}
#{{{ Variables
db_root_password="${1}"
#}}}
# Script proper
if ! is_mysql_command_available; then
echo "The MySQL/MariaDB client mysql(1) is not installed."
exit 1
fi
if is_mysql_root_password_set; then
echo "Database root password already set"
exit 0
fi
mysql --user=root <<_EOF_
UPDATE mysql.user SET Password=PASSWORD('${db_root_password}') WHERE User='root';
CREATE DATABASE typhond;
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
FLUSH PRIVILEGES;
_EOF_
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment