summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbSatellite.java
blob: f882977ccb1fbcb7fcc011125b03175c7e9a57e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.openslx.imagemaster.db;

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.encryption.AsymKeyHolder;
import org.openslx.imagemaster.thrift.iface.OrganizationData;

/**
 * Represents a satellite in the database.
 * Is used to authenticate the satellite.
 */
public class DbSatellite
{
	private static final Logger LOG = Logger.getLogger( DbSatellite.class );

	private String organizationId, address, name, publickeyString, authMethod;
	private PublicKey publickey = null;
	private List<String> suffixList = null;

	public DbSatellite( String organizationId, String address, String name, String authMethod, String publickeyString )
	{
		this.organizationId = organizationId;
		this.address = address;
		this.name = name;
		this.publickeyString = publickeyString;
	}

	public static DbSatellite fromOrganizationId( String organizationId )
	{
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"SELECT satellite.organizationid, satellite.address, satellite.name, satellite.authmethod, satellite.publickey FROM satellite WHERE satellite.organizationid = ? LIMIT 1",
						organizationId );
	}

	public static DbSatellite fromSuffix( String suffix )
	{
		return MySQL
				.findUniqueOrNull(
						DbSatellite.class,
						"SELECT satellite.organizationid, satellite.address, satellite.name, satellite.authmethod, satellite.publickey FROM satellite"
								+ " INNER JOIN satellite_suffix USING (organizationid)"
								+ " WHERE satellite_suffix.suffix = ? LIMIT 1",
						suffix );
	}

	/**
	 * Return all known satellites/organizations as List of {@link OrganizationData}, which can be
	 * used directly by the thrift API.
	 * 
	 * @return list of all known organizations/satellites
	 */
	public static List<OrganizationData> asOrganizationDataList()
	{
		List<DbSatellite> sats = MySQL.findAll(
				DbSatellite.class,
				"SELECT satellite.organizationid, satellite.address, satellite.name, satellite.authmethod, satellite.publickey FROM satellite" );
		List<OrganizationData> orgData = new ArrayList<>();
		for ( DbSatellite sat : sats ) {
			orgData.add( new OrganizationData( sat.getOrganizationId(), sat.getName(), sat.getAuthenticationMethod(), sat.getSuffixList() ) );
		}
		return orgData;
	}

	public static DbSatellite fromPrefix( String prefix )
	{
		return null;
	}

	/*
	 * Member methods
	 */

	public String getAddress()
	{
		return address;
	}

	public String getName()
	{
		return name;
	}

	public String getOrganizationId()
	{
		return organizationId;
	}

	public String getAuthenticationMethod()
	{
		return authMethod;
	}

	public List<String> getSuffixList()
	{
		if ( this.suffixList == null )
			this.suffixList = DbSatelliteSuffix.forSatellite( this );
		return this.suffixList;
	}

	/**
	 * Get the public key of this organization, if known and valid.
	 * 
	 * @return Public key, null on error or not known
	 */
	public PublicKey getPubkey()
	{
		if ( publickey == null && publickeyString != null ) {
			String parts[] = publickeyString.split( " " );
			if ( parts.length != 2 )
				return null;
			try {
				BigInteger mod = new BigInteger( parts[0] );
				BigInteger exp = new BigInteger( parts[1] );
				publickey = new AsymKeyHolder( null, exp, mod ).getPublicKey();
			} catch ( InvalidKeySpecException | NoSuchAlgorithmException e ) {
				LOG.info( "PubKey of " + this.name + " is not valid.", e );
			} catch ( NumberFormatException e ) {
				LOG.info( "PubKey of " + this.name + " is corrupted in database!", e );
			}
		}
		return publickey;
	}

}