1+ package org .opentesting .services .adapter .s3 ;
2+
3+ import java .util .Arrays ;
4+ import java .util .HashMap ;
5+ import java .util .List ;
6+
7+ import javax .annotation .PreDestroy ;
8+
9+ import org .opentesting .dto .TestCaseCheckDTO ;
10+ import org .opentesting .dto .TestCaseDTO ;
11+ import org .opentesting .dto .TestCaseInjectionDTO ;
12+ import org .opentesting .dto .TestCaseValidationDTO ;
13+ import org .opentesting .services .adapter .Adapter ;
14+ import org .opentesting .util .LogExecutionTime ;
15+ import org .opentesting .util .exceptions .ConnectFailedException ;
16+ import org .springframework .stereotype .Component ;
17+
18+ import lombok .extern .slf4j .Slf4j ;
19+
20+ @ Component
21+ @ Slf4j
22+ public class S3 extends Adapter {
23+
24+ private static final String BUCKETNAME = "bucketName" ;
25+ private static final String FILENAME = "fileName" ;
26+ private static final String REGION = "region" ;
27+
28+ //Connection cache
29+ private HashMap <String , S3Connector > s3connectors = new HashMap <>();
30+
31+ @ Override
32+ public String getServicename () {
33+ return "s3" ;
34+ }
35+
36+ @ Override
37+ public boolean inject (String testid , TestCaseInjectionDTO inject ) {
38+ try {
39+ //connect
40+ S3Config config = createConfig (inject .getService ().getConnectstring (),
41+ inject .getService ().getUsername (),
42+ inject .getService ().getPassword (),
43+ inject .getService ().getCustom (REGION ).getValue ());
44+ S3Connector connection = getConnector (testid , config );
45+ if (connection == null ) return false ;
46+
47+ //random data replacements
48+ String bucketName = this .addRandomData (inject .getService ().getCustom (BUCKETNAME ).getValue (), inject .getRandomdata ());
49+ log .debug (bucketName );
50+ String filename = this .addRandomData (inject .getService ().getCustom (FILENAME ).getValue (), inject .getRandomdata ());
51+ log .debug (filename );
52+ String data = this .addRandomData (this .getFile (testid , inject .getSourcefile ()), inject .getRandomdata ());
53+ log .debug (data );
54+
55+ //push to S3
56+ connection .store (filename , data , bucketName );
57+
58+ return true ;
59+ } catch (Exception e ) {
60+ log .error (testid +" " +inject .getInjectid ()+" " +inject .getInstanceid ()+": inject failed" , e );
61+ return false ;
62+ }
63+ }
64+
65+ @ Override
66+ public boolean check (String testid , TestCaseCheckDTO check , Object ... args ) {
67+ try {
68+ //connect
69+ S3Config config = createConfig (check .getService ().getConnectstring (),
70+ check .getService ().getUsername (),
71+ check .getService ().getPassword (),
72+ check .getService ().getCustom (REGION ).getValue ());
73+ S3Connector connection = getConnector (testid , config );
74+ if (connection == null ) return false ;
75+
76+ //random data replacements
77+ String bucketName = this .addRandomData (check .getService ().getCustom (BUCKETNAME ).getValue (), check .getRandomdata ());
78+ log .debug (bucketName );
79+ String filename = this .addRandomData (check .getService ().getCustom (FILENAME ).getValue (), check .getRandomdata ());
80+ log .debug (filename );
81+
82+ //read file from S3
83+ String result = connection .read (filename , bucketName );
84+
85+ boolean retvalue = true ;
86+
87+ //check all validations, sort first
88+ for (TestCaseValidationDTO validation : sortValidations (check .getValidations ())) {
89+ if (!validateResult (testid , check , validation , result , "S3 " +bucketName +"/" +filename )) {
90+ retvalue = false ;
91+ }
92+ }
93+
94+ return retvalue ;
95+ } catch (Exception e ) {
96+ log .error (testid +" " +check .getCheckid ()+" " +check .getInstanceid ()+": check failed" , e );
97+ return false ;
98+ }
99+ }
100+
101+ private S3Config createConfig (String connectstring , String accessKey , String secretAccessKey , String region ) {
102+ //config creation
103+ S3Config conf = new S3Config ();
104+ conf .setAccessKey (accessKey );
105+ conf .setSecretAccessKey (secretAccessKey );
106+ conf .setRegion (region );
107+ conf .setServiceEndpoint (connectstring );
108+ return conf ;
109+ }
110+
111+ /**
112+ * synchronized as we want have one connection only
113+ * @throws SQLException
114+ * @throws ClassNotFoundException
115+ */
116+ @ LogExecutionTime
117+ private synchronized S3Connector getConnector (String testid , S3Config config ) throws ConnectFailedException {
118+
119+ //no reuse because of different password configurations
120+ String key = createConnectionKey (testid , config .getServiceEndpoint (), config .getAccessKey (), config .getRegion ());
121+ S3Connector con = s3connectors .get (key );
122+
123+ //block ones
124+ if (this .isFailedConnector (key )) {
125+ log .warn (testid + ": S3 connection blocked - please check credentials and upload testcase: " + config .getAccessKey () + "@" + config .getServiceEndpoint () + "@" + config .getRegion ());
126+ return null ;
127+ }
128+
129+ try {
130+ //create new
131+ if (con == null ) {
132+ con = new S3Connector (config );
133+ log .info ("s3Connector created: " +con .getS3Config ().getServiceEndpoint () + " for " + con .getS3Config ().getAccessKey ());
134+ s3connectors .put (key , con );
135+ }
136+
137+ return con ;
138+ } catch (Exception e ) {
139+ this .addFailedConnector (key );
140+ String jdbcmessage = testid +": S3 client failed - blocked: " +config .getAccessKey () + "@" + config .getServiceEndpoint () + "@" + config .getRegion ();
141+ log .error (jdbcmessage , e );
142+ throw new ConnectFailedException (jdbcmessage );
143+ }
144+ }
145+
146+ @ Override
147+ @ LogExecutionTime
148+ public void createRequiredComponents (TestCaseDTO test ) {
149+ try {
150+ //remove blocked connections and existing connectors
151+ this .removeFailedConnectorStartingWith (test .getId ());
152+ HashMap <String , S3Connector > newConnectors = new HashMap <>();
153+ for (String key : s3connectors .keySet ()) {
154+ if (key .startsWith (test .getId ())) {
155+ s3connectors .get (key ).close ();
156+ S3Config tmp = s3connectors .get (key ).getS3Config ();
157+ log .info ("closed: " +tmp .getServiceEndpoint ()+" - " +tmp .getAccessKey ());
158+ } else {
159+ newConnectors .put (key , s3connectors .get (key ));
160+ }
161+ }
162+ s3connectors = newConnectors ;
163+ } catch (Exception e ) {
164+ log .warn ("cannot create timer" , e );
165+ }
166+ }
167+
168+ @ Override
169+ @ LogExecutionTime
170+ public List <String > getRequiredTimerCrons () {
171+ return Arrays .asList (openTestingConfig .getCheckcron ());
172+ }
173+
174+ @ PreDestroy
175+ @ LogExecutionTime
176+ private void close () {
177+ //parallel close
178+ s3connectors .values ().parallelStream ().forEach (
179+ con -> {
180+ try {
181+ con .close ();
182+ } catch (Exception e ) {
183+ log .warn ("cannot close connector" , e );
184+ }
185+ }
186+ );
187+ }
188+
189+ }
0 commit comments