View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  
35  package fr.paris.lutece.plugins.vault.business;
36  
37  import com.bettercloud.vault.VaultException;
38  import fr.paris.lutece.plugins.vault.rs.VaultAPI;
39  import fr.paris.lutece.plugins.vault.service.EnvironnementUtil;
40  import fr.paris.lutece.plugins.vault.service.VaultService;
41  import fr.paris.lutece.plugins.vault.service.VaultUtil;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.plugin.Plugin;
44  import fr.paris.lutece.portal.service.plugin.PluginService;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.util.ReferenceList;
47  
48  import java.util.List;
49  import java.util.Optional;
50  
51  /**
52   * The type Environnement home.
53   */
54  public final class EnvironnementHome
55  {
56      // Static variable pointed at the DAO instance
57      private static IEnvironnementDAO _dao = SpringContextService.getBean( "vault.environnementDAO" );
58      private static Plugin _plugin = PluginService.getPlugin( "vault" );
59  
60      private static Integer _count = 1;
61  
62      private EnvironnementHome( )
63      {
64  
65      }
66  
67      /**
68       * Create environnement.
69       *
70       * @param environnement
71       *            the environnement
72       * @return the environnement
73       */
74      public static Environnement/../../../../fr/paris/lutece/plugins/vault/business/Environnement.html#Environnement">Environnement create( Environnement environnement )
75      {
76          List<Environnement> listEnv = EnvironnementHome.getEnvironnementListByType( environnement.getType( ) );
77          environnement.setCode( environnement.getType( ) + listEnv.size( ) );
78          _dao.insert( environnement, _plugin );
79  
80          return environnement;
81      }
82  
83      /**
84       * Update environnement.
85       *
86       * @param environnement
87       *            the environnement
88       * @return the environnement
89       */
90      public static Environnement/../../../../fr/paris/lutece/plugins/vault/business/Environnement.html#Environnement">Environnement update( Environnement environnement, String strOldCode, String strOldToken) throws VaultException {
91          Application app = ApplicationHome.findByPrimaryKey( environnement.getIdapplication( ) ).get( );
92          environnement.setPath(EnvironnementUtil.getEnvironmentPath(app.getCode(),environnement.getCode()));
93          _dao.store( environnement, _plugin );
94          VaultService.getInstance().renameEnvironnement(app,environnement,strOldCode,strOldToken);
95          return environnement;
96      }
97  
98      /**
99       * Remove.
100      *
101      * @param nKey
102      *            the n key
103      */
104     public static void remove( int nKey )
105     {
106         _dao.delete( nKey, _plugin );
107     }
108 
109     /**
110      * Find by primary key optional.
111      *
112      * @param nKey
113      *            the n key
114      * @return the optional
115      */
116     public static Optional<Environnement> findByPrimaryKey( int nKey )
117     {
118         Optional<Environnement> env = _dao.load( nKey, _plugin );
119         Application app = ApplicationHome.findByPrimaryKey( env.get( ).getIdapplication( ) ).get( );
120         if ( env.isPresent( ) )
121         {
122             env.get( ).setType( env.get( ).getCode( ) );
123             env.get( ).setToken( VaultService.getInstance( ).getEnvAccessor( env.get( ).getId( ) ) );
124             env.get( ).setPath( EnvironnementUtil.getEnvironmentPath( app.getCode( ), env.get( ).getCode( ) ) );
125             env.get( ).setListProperties( VaultService.getInstance( ).getSecretsByEnv( app, env.get( ) ) );
126         }
127         return env;
128     }
129 
130     /**
131      * Gets environnements list.
132      *
133      * @return the environnements list
134      */
135     public static List<Environnement> getEnvironnementsList( )
136     {
137         List<Environnement> envs = _dao.selectEnvironnementsList( _plugin );
138         if ( !envs.isEmpty( ) )
139         {
140             envs.forEach( x -> x.setToken( VaultService.getInstance( ).getEnvAccessor( x.getId( ) ) ) );
141             envs.forEach( x -> x.setPath(
142                     EnvironnementUtil.getEnvironmentPath( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ).getCode( ), x.getCode( ) ) ) );
143             envs.forEach( x -> x
144                     .setListProperties( VaultService.getInstance( ).getSecretsByEnv( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ), x ) ) );
145 
146         }
147         return envs;
148     }
149 
150     /**
151      * Gets id environnements list.
152      *
153      * @return the id environnements list
154      */
155     public static List<Integer> getIdEnvironnementsList( )
156     {
157         return _dao.selectIdEnvironnementsList( _plugin );
158     }
159 
160     /**
161      * Gets environnements reference list.
162      *
163      * @return the environnements reference list
164      */
165     public static ReferenceList getEnvironnementsReferenceList( )
166     {
167         return _dao.selectEnvironnementsReferenceList( _plugin );
168     }
169 
170     /**
171      * Gets environnements list by ids.
172      *
173      * @param listIds
174      *            the list ids
175      * @return the environnements list by ids
176      */
177     public static List<Environnement> getEnvironnementsListByIds( List<Integer> listIds )
178     {
179         List<Environnement> listEnvs = _dao.selectEnvironnementsListByIds( _plugin, listIds );
180         if ( !listEnvs.isEmpty( ) )
181         {
182             listEnvs.forEach( x -> {
183                 x.setPath( EnvironnementUtil.getEnvironmentPath( ApplicationHome.findByPrimaryKey( x.getIdapplication( ) ).get( ).getCode( ), x.getCode( ) ) );
184             } );
185         }
186         return listEnvs;
187     }
188 
189     /**
190      * Gets id environnements list by app.
191      *
192      * @param idApp
193      *            the id app
194      * @return the id environnements list by app
195      */
196     public static List<Integer> getIdEnvironnementsListByApp( Integer idApp )
197     {
198         return _dao.selectIdEnvironnementByIdApp( idApp, _plugin );
199     }
200 
201     /**
202      * Gets properties list.
203      *
204      * @param nIdEnv
205      *            the n id env
206      * @return the properties list
207      */
208     public static List<Properties> getPropertiesList( Integer nIdEnv )
209     {
210         Environnement env = EnvironnementHome.findByPrimaryKey( nIdEnv ).get( );
211         Application app = ApplicationHome.findByPrimaryKey( env.getIdapplication( ) ).get( );
212         return VaultService.getInstance( ).getSecretsByEnv( app, env );
213     }
214 
215     /**
216      * Gets environnement list by type.
217      *
218      * @param type
219      *            the type
220      * @return the environnement list by type
221      */
222     public static List<Environnement> getEnvironnementListByType( String type )
223     {
224         return _dao.selectEnvironnementByType( type, _plugin );
225     }
226 
227 }