summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/db/DbSatellite.java
blob: 4d70bef987c811a6cfc29bffbd0598c8704ab294 (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
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.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;
	private PublicKey publickey = null;

	public DbSatellite( String organizationId, String address, String name, 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.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.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()
	{
		return MySQL.findAll( OrganizationData.class, "SELECT satellite.organizationid, satellite.name, satellite.authmethod FROM satellite" );
	}

	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;
	}

	/**
	 * 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;
	}

}